Javascript 在新窗口中打开 iframe 内的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4583792/
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
Make links inside an iframe open in a new window
提问by Breezer
I'm trying to display an map inside an iframe that has a needle which on mouseover shows some information about a certain company, the thing is once you click the link the page opens up inside the iframe which ruins the experience, so i was wondering is it possible to make the links inside an iframe open up in a new window instead perhaps using jquery or something similiar?
我正在尝试在 iframe 中显示一张地图,该地图有一个指针,鼠标悬停时会显示有关某个公司的一些信息,问题是一旦您单击链接,页面就会在 iframe 内打开,这会破坏体验,所以我想知道是否可以在新窗口中打开 iframe 内的链接,而不是使用 jquery 或类似的东西?
the code i have atm is
我有 atm 的代码是
http://www.jsfiddle.net/rkd59/1/
http://www.jsfiddle.net/rkd59/1/
Edit: the very least capture a click within the iframe so i might resize the iframe
编辑:至少在 iframe 中捕获一次点击,所以我可能会调整 iframe 的大小
回答by mekwall
You will need some kind of open API to do this properly, and Enirodoesn't provide one according to this page(in Swedish).
您将需要某种开放的 API 才能正确执行此操作,而Eniro未根据此页面(瑞典语)提供一个。
I would recommend you to use the Google Maps API v3instead. I've made an example on jsFiddlethat looks similar to that of Eniro.
我建议您改用Google Maps API v3。我在 jsFiddle 上做了一个与 Eniro 相似的例子。
I'll gladly give you more help with this, so just ask away!
我很乐意在这方面为您提供更多帮助,所以尽管问吧!
回答by Yves Van Broekhoven
Due this map is loaded inside an iFrame, it's not possible to run any javascript event listeners on the links, neither is it possible to change the html.
由于此地图加载在 iFrame 内,因此无法在链接上运行任何 javascript 事件侦听器,也无法更改 html。
回答by JakeParis
You can't (or it is extremelyhard to ) make events inside the iframe affect the parent page. This is to prevent attacks from XSS, or cross site scripting. Having said that, if the site within the iframe is on your own domainand you want to set up some extremely tricky ajaxing and php session IDs, maybeyou could make something work, but even then I'm not sure. And I don't know if this would be a security hole, maybe someone else can speak to that. It would perhaps look like:
你不能(或者它是非常难)让事件中的iframe影响父页面。这是为了防止来自 XSS 或跨站点脚本的攻击。话虽如此,如果 iframe 中的站点在您自己的域中,并且您想设置一些非常棘手的 ajaxing 和 php 会话 ID,也许您可以做一些工作,但即便如此,我也不确定。我不知道这是否会是一个安全漏洞,也许其他人可以谈谈。它可能看起来像:
- main page sets up session ID and passes that to the iframe url via a get variable
- the iframe takes click information and sends it to a Session variable via an ajaxing call to a script on the server.
- The main page then reads (how?) the session cookie and makes changes based on it's value.
- 主页设置会话 ID 并通过 get 变量将其传递给 iframe url
- iframe 获取点击信息,并通过对服务器上脚本的 ajaxing 调用将其发送到 Session 变量。
- 然后主页面读取(如何?)会话 cookie 并根据它的值进行更改。
All in all, you may find that it may be much simpler and more secure to acheive what you want using a different method.
总而言之,您可能会发现使用不同的方法实现您想要的目标可能更简单、更安全。
回答by m.edmondson
To make a link popup in a new window you would usually use target="_blank"
as such:
要在新窗口中弹出链接,您通常会使用target="_blank"
:
<a href="http://www.yahoo.com" target="_blank">Go to Yahoo</a>
However this will only work if you can modify the code you're showing within the iFrame
但是,这仅在您可以修改在 iFrame 中显示的代码时才有效
回答by Steven de Salas
There is a partial solution.
有一个部分解决方案。
You can add an absolutely positioned DIV tag over the top of the IFRAME and capture clicks on this instead. See example here shaded in 20% alpha red.
您可以在 IFRAME 顶部添加一个绝对定位的 DIV 标签,并改为捕获点击次数。请参见此处以 20% alpha 红色着色的示例。
http://www.jsfiddle.net/rkd59/6/
http://www.jsfiddle.net/rkd59/6/
However, this means that the map works in "read-only mode" and while you can capture the click event you wont know what link the user has clicked on.
但是,这意味着地图在“只读模式”下工作,虽然您可以捕获点击事件,但您不知道用户点击了哪个链接。
回答by Justin Levene
Please try the following:
请尝试以下操作:
<script>
x=document.querySelectorAll("a");
for(i=0;i<x.length;i++)
{
x[i].setAttribute("target","_blank");
}
</script>
Thus all links open in new frame.
因此,所有链接都在新框架中打开。