简体   繁体   English

如何防止 iframe 在新标签页中打开

[英]How to prevent iframe from opening in a new tab

I have a iframe, in that iframe, there is links inside the website that if you click it, it opens in a new tab.我有一个 iframe,在那个 iframe 中,网站内有链接,如果单击它,它将在新选项卡中打开。 How do I disable the "open in a new tab", and make it to just open in the same iframe?如何禁用“在新选项卡中打开”,并使其仅在同一个 iframe 中打开? I have looked all around Stack Overflow, and there are no answers that actaully work.我环顾四周 Stack Overflow,并没有真正有效的答案。 Thanks in advance.提前致谢。

If you have control over the code inside the iframe website, you would need to set the href tags to have target properties of _self .如果您可以控制 iframe 网站内的代码,则需要将href标记设置为具有_self的目标属性。 Like the following:如下所示:

  <a href="http://www.stackoverflow.com" target="_self">stackoverflow</a>

If you cannot change to code of the iframe website, you could still run a javascript or jQuery script to add that property if the iframe website is loaded from the same domain as your website. If you cannot change to code of the iframe website, you could still run a javascript or jQuery script to add that property if the iframe website is loaded from the same domain as your website.

$(document).ready(function(){
var $iframe = $("iframe");
    $('#frame').on('load', function() {
       $(this).contents().find("a").attr('target', '_self');
    });
});

This will find hyperlinks and set the target attribute to _self which instructs the browser to open the link in the same window.这将找到超链接并将目标属性设置为_self ,这会指示浏览器在同一 window 中打开链接。

If your website is from domain A (eg. mysite.com) and the iframe is from domain B (eg. othersite.com), web browser security prevents javascript to manipulate the iframe content. If your website is from domain A (eg. mysite.com) and the iframe is from domain B (eg. othersite.com), web browser security prevents javascript to manipulate the iframe content.

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

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