javascript 如何防止 iframe 中的链接在新标签页中打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29188009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent links in iframe from opening in new tab
提问by UltraStallion
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>
回答by Cisc
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.
我还没有测试过,但我希望它有效。
回答by Canvas
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 内容是跨域的,我认为您根本无法编辑任何代码。
回答by jdnoon
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)
_blank = Opens the linked document in a new window or tab
_parent = Opens the linked document in the parent frame
_top = Opens the linked document in the full body of the window
framename = Opens the linked document in a named frame
_self = 在单击时在同一框架中打开链接文档(这是默认设置)
_blank = 在新窗口或选项卡中打开链接文档
_parent = 在父框架中打开链接的文档
_top = 在整个窗口中打开链接的文档
framename = 在命名框架中打开链接文档
Information from:
资料来自: