javascript iframe:我们如何从 iframe 内部更改 iframe src?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20788075/
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
iframe: How can we change iframe src from inside iframe itself?
提问by user3137239
I know there are question have been asked around resembling to this one, but still I am not getting proper concept of this. I am currently loading an iframe through javascript in following manner:
我知道有人问过类似这个的问题,但我仍然没有得到正确的概念。我目前正在以下列方式通过 javascript 加载 iframe:
jQuery('.out-div-login').html("<iframe id='parent' frameBorder='0' width='320' height='500' src='"+_url+"'></iframe>");
Which is working fine. (its cross domain). After loading my application inside given iframe, I want to come back to original state where .out-div-login
was loading iframe into parent html.
From outside of iframe I can do this by accessing iframe using its id
attribute, but not from the inside. Is there any way I can reload the iframe by giving its src again ? or by above code but from inside the iframe ? Thanks.
Update
I have tried below code without any success as of now:
哪个工作正常。(它的跨域)。在给定的 iframe 中加载我的应用程序后,我想回到.out-div-login
将 iframe 加载到父 html 的原始状态。
从 iframe 外部,我可以通过使用 iframe 的id
属性访问 iframe 来做到这一点,但不能从内部访问。有什么办法可以通过再次提供它的 src 来重新加载 iframe 吗?或者通过上面的代码但是从 iframe 内部?谢谢。
更新
我已经尝试了以下代码,但目前没有任何成功:
var ifr = $('iframe[id="parent"]');
from one of js file inside iframe
with id parent
. when I do console.log(ifr)
it gives in firebug something like this Object[]
instead of <iframe id="parent">
来自其中一个iframe
id的 js 文件parent
。当我这样做时,console.log(ifr)
它会给萤火虫这样的东西Object[]
而不是<iframe id="parent">
回答by Sebi2020
You should use location.href
inside the iframe. That should work. set location.href in the iframe to your new location.
您应该location.href
在 iframe 内使用。那应该有效。将 iframe 中的 location.href 设置为您的新位置。
回答by Yao
The url query string (?xxx) should also be kept in the new url, so the whole solution should be:
url查询字符串(?xxx)也应该保留在新的url中,所以整个解决方案应该是:
location.href = "new_link_url" + location.search;