How can I reload iframe using jquery?
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js" > </script>
<script type="text/javascript">
//Reload Iframe Function
</script>
</head>
<body>
<iframe name="f1" id = "f1" src="http://www.google.com.pk/search?q=usa+current+time&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGLL_en" width=500px height=250px>
</iframe>
<button onClick="abc()"> Reload </button>
</body>
</html>
Based on your comment to Haim's post, what you want could be simplified as:
$('#reload').click(function() {
$('#f1').attr('src', $('#f1').attr('src'));
return false;
});
Or
$('#reload').click(function() {
$('#f1')[0].contentWindow.location.reload(true);
return false;
});
But perhaps his answers better suits your needs.
UPDATE:
I put together a working example using the method I provided above. I have an <iframe>
in one page that shows another page that prints the time.
The <iframe>
HTML (time.html):
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
The time is now <span id="time"></span>.
<script type="text/javascript">
$(document).ready(function() {
$('#time').html((new Date()).toString());
});
</script>
</body>
And the parent page HTML (test.html):
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<iframe id="frame" src="file:///D:/Users/Cory/Desktop/time.html" width="500" height="250"></iframe>
<button id="reload">Refresh</button>
<script type="text/javascript">
$(document).ready(function() {
$('#reload').click(function() {
$('#frame').attr('src', $('#frame').attr('src'));
return false;
});
});
</script>
</body>
This works just fine for me in Firefox 3.6 and IE 7.
$("#reload").click(function() {
jQuery.each($("iframe"), function() {
$(this).attr({
src: $(this).attr("src")
});
});
return false;
});
This will loop through each iFrame every time you click on a link with a class of reloadAds and set the source of the iFrame to itself
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With