Javascript 从 iframe 更改父 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11207900/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:54:41  来源:igfitidea点击:

Change Parent url from iframe

javascriptiframeparent

提问by user1476899

Need help with a simple problem! which has the following criteria:

需要帮助解决一个简单的问题!它具有以下标准:

1) click on images link in iframe changes Parent page, click on another iframe image link changes Parent to another page (see below).

1)点击iframe中的图片链接更改Parent页面,点击另一个iframe图片链接将Parent更改为另一个页面(见下文)。

It may sound simple but I'm being googled it for days now and looked over many forums. Code can be in html css or js, but please keep any answers simple as possible and post a full working example to work as I'm new to coding or recode the test site: http://www.howiepotter.com/parent.html

这听起来可能很简单,但我已经在谷歌上搜索了好几天并查看了许多论坛。代码可以是 html css 或 js,但请尽可能保持任何答案简单,并发布一个完整的工作示例,因为我是编码或重新编码测试站点的新手:http: //www.howiepotter.com/parent。 html

enter image description here

在此处输入图片说明

回答by biziclop

http://reference.sitepoint.com/html/a/target

http://reference.sitepoint.com/html/a/target

"_top"

loads content in the top-level frameset (in effect, the whole browser window), no matter how many nested levels down the current frame is located

“_最佳”

加载顶级框架集中的内容(实际上是整个浏览器窗口),无论当前框架下有多少嵌套级别

<a href="page" target="_top">Replace parent url!</a>

回答by jbabey

Change your link from this:

从此更改您的链接:

<a href="link-here.html">

To this:

对此:

<a href="#" onclick="top.window.location.href='yourURL';">

If you want, you could just put the onclickhandler on the image instead and get rid of the anchor.

如果你愿意,你可以把onclick处理程序放在图像上,然后去掉锚点。

Note that this is not the correct place to have javascript (handlers should be bound from a .js file, not in markup), but i get the feeling you are looking for a surgical answer and don't care much for best practices.

请注意,这不是使用 javascript 的正确位置(处理程序应该从 .js 文件中绑定,而不是在标记中),但我感觉您正在寻找外科手术式的答案并且不太关心最佳实践。

edit: as Victor Nicollet pointed out, this will throw a security exception if your iframe and parent page do not share domains. see http://en.wikipedia.org/wiki/Same_origin_policy

编辑:正如 Victor Nicollet 指出的那样,如果您的 iframe 和父页面不共享域,这将引发安全异常。见http://en.wikipedia.org/wiki/Same_origin_policy

回答by Pixelomo

In case @biziclop decides to delete his answer as he seems to be threatening to do in the comments, here's his answer again which is very useful:

如果@biziclop 决定删除他的答案,因为他似乎在评论中威胁要这样做,那么他的答案再次非常有用:

http://reference.sitepoint.com/html/a/target

http://reference.sitepoint.com/html/a/target

"_top"

loads content in the top-level frameset (in effect, the whole browser window), no matter how many nested levels down the current frame is located

“_最佳”

加载顶级框架集中的内容(实际上是整个浏览器窗口),无论当前框架下有多少嵌套级别

<a href="page" target="_top">Replace parent url!</a>