Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload an iframe with jQuery

People also ask

How to refresh the iFrame in jQuery?

Simple jQuery code snippet to reload an iFrame by accessing the contentWindow property to obtain the location object. The jQuery snippet code actually loops through and refreshes all the iFrames on the page.

How do I refresh an iFrame?

Appending an empty string to the src attribute of the iFrame also reloads it automatically. This is the best answer because it works on all iframes, including cross-origin iframes.


$( '#iframe' ).attr( 'src', function ( i, val ) { return val; });

If the iframe was not on a different domain, you could do something like this:

document.getElementById(FrameID).contentDocument.location.reload(true);

But since the iframe is on a different domain, you will be denied access to the iframe's contentDocument property by the same-origin policy.

But you can hackishly force the cross-domain iframe to reload if your code is running on the iframe's parent page, by setting it's src attribute to itself. Like this:

// hackishly force iframe to reload
var iframe = document.getElementById(FrameId);
iframe.src = iframe.src;

If you are trying to reload the iframe from another iframe, you are out of luck, that is not possible.


you can also use jquery. This is the same what Alex proposed just using JQuery:

 $('#currentElement').attr("src", $('#currentElement').attr("src"));

Reload an iframe with jQuery

make a link inside the iframe lets say with id = "reload" then use following code

$('#reload').click(function() {
    document.location.reload();
});

and you are good to go with all browsers.

Reload an iframe with HTML (no Java Script req.)

It have more simpler solution: which works without javaScript in (FF, Webkit)

just make an anchor inSide your iframe

   <a href="#" target="_SELF">Refresh Comments</a>

When you click this anchor it just refresh your iframe

But if you have parameter send to your iframe then do it as following.

  <a id="comnt-limit" href="?id=<?= $page_id?>" target="_SELF">Refresh Comments</a>

do not need any page url because -> target="_SELF" do it for you


with jquery you can use this:

$('#iframeid',window.parent.document).attr('src',$('#iframeid',window.parent.document).attr('src'));

Just reciprocating Alex's answer but with jQuery

var currSrc = $("#currentElement").attr("src");
$("#currentElement").attr("src", currSrc);

Here is another way

$( '#iframe' ).attr( 'src', function () { return $( this )[0].src; } );