如何在新标签中打开iframe中的所有链接 [英] How to make all links in an iframe open in new tab

查看:491
本文介绍了如何在新标签中打开iframe中的所有链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到这一点,当你点击< iframe> 中的链接时,它会打开新标签中的链接 - 而不是< IFRAME> 。请注意,我无法控制人们通过< iframe> 查看的内容。

我试图用javascript来做到这一点,就像 $('a')。setAttribute('target','_ blank') ;
但它不适合我。
我在我的HTML文档中也有< base target =_ blank> ,这也不算什么。

b

这是不可能的吗?或者我只是在错误的地方搜索?
提前致谢!

解决方案

我期望得到这样的结果:

  $( 'a')中的setAttribute( '目标', '_空白'); 

..失败(默默地 - 因为jquery默默地失败),因为如果iFrame的内容



即:如果您自己的页面来自域a(例如,来自不同的域),则存在访问问题,即从包含页面操纵iFrame中的页面。 mysite.com),并且iframe来自域b(例如someothersite.com),那么web浏览器的安全行为是这样的,即使用javascript来操纵iFrame内容会导致访问被拒绝错误。



如果您要使用非jQuery的javascript,您会看到错误。从父页面,这里有一些东西:

  window.frames [iFrameName]。getElementsByTagName(a)[0 ] .target =_ blank

你会看到错误信息。 b

不幸的是,我不确定你可以对此做些什么。这是一个故意的事情,停止一个网站,包括另一个,并加载后更改内容,以便它说别的。



解决方法之一就是调用脚本你的服务器(即同一个域名)并传入一个URL。该脚本获取您所浏览页面的内容,并将其返回到您的浏览器,因此您将拥有所需网页的内容,但其地址位于您的网站内。例如,如果它是php:



http:// yourdomain。 com / getURL.php?url = http:www.google.com



请注意有关身份验证的问题


I'm trying to make it so when you click on a link in an <iframe> it will open the link in a new tab - not in the <iframe>. Please note I do not have control of the content the people are viewing through the <iframe>.

I'm trying to do this with javascript possibly, like $('a').setAttribute('target','_blank'); but it's not working out for me. I also have in my HTML document <base target="_blank">, that is not doing the trick too.

Is this impossible? Or am I just searching in the wrong places? Thanks in advance!

解决方案

I'd expect this :

$('a').setAttribute('target','_blank');

.. to fail (silently - because jquery generally fails silently) because, if the content of the iFrame is from a different Domain, there are access issues with manipulating the page within the iFrame from the containing page.

That is : If your own page is from domain a (eg mysite.com), and the iframe is from domain b (eg someothersite.com), then web browser security behaviour is such that using javascript to manipulate the iFrame content will give an "Access is denied" error.

If you were to use non-jquery javascript you'll see the error. From the parent page, something likie this :

window.frames["iFrameName"].getElementsByTagName("a")[0].target="_blank"

you'll see the error.

Unfortunately I'm not sure what you can do about this. It's a deliberate thing to stop one website including another, and changing the content after loading so that it says something else.

One way around it would be to call a script on your server (ie same domain) and pass in a URL. The script gets the content of the page you were after and regurgitates it to your browser, so you'll have the content of your desired webpage but the address of it was within your site.. eg if it were php:

http://yourdomain.com/getURL.php?url=http:www.google.com

Watch out for issues with authentication there though

这篇关于如何在新标签中打开iframe中的所有链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆