Reload an iframe with jQuery

2019-01-01 05:26发布

I have two iframes on a page and one makes changes to the other but the other iframe doesn't show the change until I refresh. Is there an easy way to refresh this iframe with jQuery?

<div class="project">
  <iframe id="currentElement" class="myframe" name="myframe" src="http://somesite.com/something/new"></iframe>
</div>

17条回答
浅入江南
2楼-- · 2019-01-01 05:51

Just reciprocating Alex's answer but with jQuery

var currSrc = $("#currentElement").attr("src");
$("#currentElement").attr("src", currSrc);
查看更多
呛了眼睛熬了心
3楼-- · 2019-01-01 05:52

Just reciprocating Alex's answer but with jQuery:

var e = $('#myFrame');
    e.attr("src", e.attr("src"));

Or you can use small function:

function iframeRefresh(e) {
    var c = $(e);
        c.attr("src", c.attr("src"));
}

I hope it helps.

查看更多
浪荡孟婆
4楼-- · 2019-01-01 05:53

I'm pretty sure all of the examples above only reload the iframe with its original src, not its current URL.

$('#frameId').attr('src', function () { return $(this).contents().get(0).location.href });

That should reload using the current url.

查看更多
忆尘夕之涩
5楼-- · 2019-01-01 05:54

with jquery you can use this:

$('#iframeid',window.parent.document).attr('src',$('#iframeid',window.parent.document).attr('src'));
查看更多
墨雨无痕
6楼-- · 2019-01-01 05:55

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.

查看更多
谁念西风独自凉
7楼-- · 2019-01-01 05:55

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

查看更多
登录 后发表回答