Javascript 使用 jQuery 重新加载 iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4249809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload an iframe with jQuery
提问by Matt Elhotiby
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?
我在一个页面上有两个 iframe,一个对另一个进行了更改,但另一个 iframe 在我刷新之前不会显示更改。有没有一种简单的方法可以用 jQuery 刷新这个 iframe?
<div class="project">
<iframe id="currentElement" class="myframe" name="myframe" src="http://somesite.com/something/new"></iframe>
</div>
回答by ?ime Vidas
$( '#iframe' ).attr( 'src', function ( i, val ) { return val; });
回答by Alex
If the iframe was not on a different domain, you could do something like this:
如果 iframe 不在不同的域中,您可以执行以下操作:
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.
但是由于 iframe 位于不同的域中,因此同源策略将拒绝您访问 iframe 的contentDocument
属性。
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:
但是,如果您的代码在 iframe 的父页面上运行,您可以通过将其 src 属性设置为自身来强制重新加载跨域 iframe。像这样:
// 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, thatis notpossible.
如果您尝试从另一个 iframe 重新加载 iframe,那您就不走运了,这是不可能的。
回答by ynh
you can also use jquery. This is the same what Alex proposed just using JQuery:
你也可以使用jquery。这与 Alex 仅使用 JQuery 提出的建议相同:
$('#currentElement').attr("src", $('#currentElement').attr("src"));
回答by ynh
Reload an iframe with jQuery
使用 jQuery 重新加载 iframe
make a link inside the iframe lets say with id = "reload" then use following code
在 iframe 内创建一个链接,让我们说 id = "reload" 然后使用以下代码
$('#reload').click(function() {
document.location.reload();
});
and you are good to go with all browsers.
并且您可以使用所有浏览器。
Reload an iframe with HTML (no Java Script req.)
使用 HTML 重新加载 iframe(无 Java Script 要求)
It have more simpler solution: which works without javaScript in (FF, Webkit)
它有更简单的解决方案:在(FF,Webkit)中没有javaScript的情况下工作
just make an anchor inSide your iframe
只需在您的 iframe 内制作一个锚点
<a href="#" target="_SELF">Refresh Comments</a>
When you click this anchor it just refresh your iframe
当您单击此锚点时,它只会刷新您的 iframe
Butif you have parameter send to your iframe then do it as following.
但是,如果您有参数发送到您的 iframe,请按以下方式操作。
<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
不需要任何页面 url 因为 -> target="_SELF" 为你做
回答by elin3t
with jquery you can use this:
使用 jquery,你可以使用这个:
$('#iframeid',window.parent.document).attr('src',$('#iframeid',window.parent.document).attr('src'));
回答by Matt Asbury
Just reciprocating Alex's answer but with jQuery
只是用 jQuery 来回报 Alex 的回答
var currSrc = $("#currentElement").attr("src");
$("#currentElement").attr("src", currSrc);
回答by Mark
I'm pretty sure all of the examples above only reload the iframe with its original src, not its current URL.
我很确定上面的所有示例仅使用其原始 src 重新加载 iframe,而不是其当前 URL。
$('#frameId').attr('src', function () { return $(this).contents().get(0).location.href });
That should reload using the current url.
那应该使用当前的 url 重新加载。
回答by Nasir Mahmood
Here is another way
这是另一种方式
$( '#iframe' ).attr( 'src', function () { return $( this )[0].src; } );
回答by Pavel
$('IFRAME#currentElement').get(0).contentDocument.location.reload()
回答by Akm16
Just reciprocating Alex's answer but with jQuery:
只是用 jQuery 来回报 Alex 的回答:
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.
我希望它有帮助。