简体   繁体   English

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

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

I made a little web-based web browser for a web-based OS I made.我为我制作的基于网络的操作系统制作了一个基于网络的网络浏览器。 I noticed that in some sites, they have links that like to open in new tabs.我注意到在某些网站中,他们有喜欢在新标签页中打开的链接。 Is there a way that this can be prevented and have the links open in the iframe instead?有没有办法可以防止这种情况并在 iframe 中打开链接?

Here's my code for the whole browser just in case:这是我的整个浏览器的代码,以防万一:

<html>
<head>

<link href="./browser.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript"  src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$("#load").click(function() {
var new_url = $("#url").val();

// Checks that the user typed "http://" or not
if(new_url.substr(0,7)!="http://")
new_url = "http://"+new_url;

$("#main_frame").attr("src", new_url);
});
});
</script>
</head>
<body>

<div id="help">
<form action="help.html"><input type="submit" value="Help"></form>
</div>

Address:
<div id="logo">
<img src="stallion.png">
</div>
<input type="text" style="width: 400px;" name="url" id="url">
<input type="button" value="Go" id="load">

<div>
<input type="image" src="back.png" height=25 width=25 onclick="back()">
<input type="image" src="forward.png" height=25 width=25 onclick="forward()">
<input type="image" src="refresh.png" height=26 width=26 onclick="refresh()">
</div>

<iframe frameborder=0 class="netframe" src="http://www.bing.com/" id="main_frame"></iframe>

</body>
<script>
function back()
{
window.history.back();
}
</script>

<script>
function forward()
{
window.history.forward();
}
</script>

<script>
function refresh()
{
var iframe = document.getElementById('main_frame');
iframe.src = iframe.src;
}
</script>

</html>

There is two choices有两个选择

1 : You can check all of the html in the iframe on each change and look for "target=_blank" and if so replace with "target=_self" 1:您可以在每次更改时检查 iframe 中的所有 html 并查找"target=_blank" ,如果是,则替换为"target=_self"

2 : Which I think would be the better way is, when the user clicks on an anchor tag check to see if the anchor has the attribute "target=_blank" if they do, simply remove it and then click the link. 2:我认为更好的方法是,当用户单击锚标记时,检查锚是否具有属性"target=_blank"如果有),只需将其删除,然后单击链接。

I have provided a jsFiddle below我在下面提供了一个 jsFiddle

https://jsfiddle.net/L5dhp80e/ https://jsfiddle.net/L5dhp80e/

Html html

<a class="newTab" href="http://www.google.com" target="_blank">
    New Tab Google
</a>

<br />
<a class="removeBlank" href="http://www.google.com" target="_blank">
    Removed Target Blank Google
</a>

Javascript Javascript

$(function () {
    $('a.removeBlank').on('click', function () {

        if ($(this).attr('target') == "_blank") {
            $(this).attr('target', '_self');
        }

        $(this).click();

        return false;
    })
});

However if the iframe content is cross domain I don't think you can edit any of the code at all.但是,如果 iframe 内容是跨域的,我认为您根本无法编辑任何代码。

Get DOM content of cross-domain iframe 获取跨域iframe的DOM内容

I found this answer in the web:我在网上找到了这个答案:

window.open = function (url, name, features, replace) {
    document.getElementById('#iframe').src = url;
}

or with jQuery:或使用 jQuery:

window.open = function (url, name, features, replace) {
    $('#iframe').attr('src', url);
}

I haven't already tested it, but i hope it works.我还没有测试过,但我希望它有效。

Add target="_self"添加target="_self"

<iframe src="http://google.com" target="_self"></iframe>

Target Attributes are:目标属性是:

_self = Opens the linked document in the same frame as it was clicked (this is default) _self = 在单击时在同一框架中打开链接文档(这是默认设置)

_blank = Opens the linked document in a new window or tab _blank = 在新窗口或选项卡中打开链接的文档

_parent = Opens the linked document in the parent frame _parent = 在父框架中打开链接的文档

_top = Opens the linked document in the full body of the window _top = 在整个窗口中打开链接的文档

framename = Opens the linked document in a named frame framename = 在命名框架中打开链接文档

Information from:资料来自:

http://www.w3schools.com/tags/att_a_target.asp http://www.w3schools.com/tags/att_a_target.asp

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

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