我为我制作的基于网络的操作系统制作了一个基于网络的小型网络浏览器。我注意到在某些网站中,它们有喜欢在新选项卡中打开的链接。有没有办法可以防止这种情况并在 iframe 中打开链接?
这是我的整个浏览器的代码,以防万一:
<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>
有两个选择
1:您可以在每次更改时检查 iframe 中的所有 html 并查找"target=_blank"
如果是这样替换为"target=_self"
2:我认为更好的方法是,当用户单击锚标记时检查锚是否具有该属性"target=_blank"
如果他们这样做,只需将其删除,然后单击链接即可。
我在下面提供了一个 jsFiddle
https://jsfiddle.net/L5dhp80e/ https://jsfiddle.net/L5dhp80e/
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
$(function () {
$('a.removeBlank').on('click', function () {
if ($(this).attr('target') == "_blank") {
$(this).attr('target', '_self');
}
$(this).click();
return false;
})
});
但是,如果 iframe 内容是跨域的,我认为您根本无法编辑任何代码。
获取跨域iframe的DOM内容 https://stackoverflow.com/questions/6170925/get-dom-content-of-cross-domain-iframe
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)