简体   繁体   English

在新窗口中打开iframe内的链接

[英]Make links inside an iframe open in a new window

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的大小

You will need some kind of open API to do this properly, and Eniro doesn't provide one according to this page (in Swedish). 您需要某种开放的API来正确执行此操作, Eniro不会根据此页面提供一个(瑞典语)。

I would recommend you to use the Google Maps API v3 instead. 我建议您改用Google Maps API v3 I've made an example on jsFiddle that looks similar to that of Eniro. 在jsFiddle上做了一个类似于Eniro的例子

I'll gladly give you more help with this, so just ask away! 我很乐意为你提供更多帮助,所以请你离开!

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中显示的代码时,这才有效

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. 因此,所有链接都在新框架中打开。

由于此映射是在iFrame中加载的,因此无法在链接上运行任何javascript事件侦听器,也无法更改html。

You can't (or it is extremely hard to ) make events inside the iframe affect the parent page. 你不能(或者它是非常难)使iframe中的事件影响的父页面。 This is to prevent attacks from XSS, or cross site scripting . 这是为了防止来自XSS或跨站点脚本的攻击。 Having said that, if the site within the iframe is on your own domain and you want to set up some extremely tricky ajaxing and php session IDs, maybe you could make something work, but even then I'm not sure. 话虽如此,如果iframe中的网站位于您自己的域中并且您想要设置一些非常棘手的ajaxing和php会话ID, 也许您可以创建一些工作,但即便如此我也不确定。 And I don't know if this would be a security hole, maybe someone else can speak to that. 而且我不知道这是否会成为一个安全漏洞,也许其他人可以对此说话。 It would perhaps look like: 它可能看起来像:

  1. main page sets up session ID and passes that to the iframe url via a get variable 主页面设置会话ID,并通过get变量将其传递给iframe网址
  2. the iframe takes click information and sends it to a Session variable via an ajaxing call to a script on the server. iframe获取点击信息,并通过对服务器上脚本的ajaxing调用将其发送到Session变量。
  3. The main page then reads (how?) the session cookie and makes changes based on it's value. 然后主页面读取(如何?)会话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. 总而言之,您可能会发现使用不同的方法实现您想要的功能可能更简单,更安全。

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. 您可以在IFRAME的顶部添加绝对定位的DIV标记,并获取对此的点击。 See example here shaded in 20% alpha red. 请参见此处以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. 但是,这意味着地图以“只读模式”工作,虽然您可以捕获点击事件,但您不知道用户点击了哪个链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM