如何在新选项卡中打开 iframe 中的所有链接 [英] How to make all links in an iframe open in new tab
问题描述
我正在尝试这样做,当您单击 <iframe>
中的链接时,它将在新选项卡中打开该链接 - 而不是 <iframe>代码>.请注意,我无法控制人们通过
查看的内容.
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>
.
我可能正在尝试使用 javascript 来执行此操作,例如 $('a').setAttribute('target','_blank');
但这对我不起作用.我的 HTML 文档中也有 <base target="_blank">
,这也不起作用.
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!
推荐答案
我希望:
$('a').setAttribute('target','_blank');
.. 失败(静默 - 因为 jquery 通常会静默失败)因为,如果 iFrame 的内容来自不同的域,则从包含页面操作 iFrame 内的页面会出现访问问题.
.. 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.
也就是说:如果您自己的页面来自域 a(例如 mysite.com),而 iframe 来自域 b(例如 someothersite.com),那么网络浏览器的安全行为就是使用 javascript 来操作 iFrame 内容将给出访问被拒绝"错误.
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.
如果您要使用非 jquery javascript,您将看到错误.从父页面,类似这样的东西:
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"
你会看到错误.
不幸的是,我不确定您能对此做些什么.停止一个网站包括另一个网站,并在加载后更改内容以使其显示其他内容是故意的.
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.
一种解决方法是在您的服务器(即相同域)上调用脚本并传入一个 URL.该脚本获取您访问的页面的内容并将其反刍到您的浏览器,因此您将获得所需网页的内容,但其地址位于您的网站内.. 例如,如果它是 php:
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屋!