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

Sea*_*uy4 5 html javascript iframe jquery

我为我制作的基于网络的操作系统制作了一个基于网络的网络浏览器。我注意到在某些网站中,他们有喜欢在新标签页中打开的链接。有没有办法可以防止这种情况并在 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>
Run Code Online (Sandbox Code Playgroud)

小智 -2

我在网上找到了这个答案:

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

或者使用 jQuery:

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

我还没有测试过,但我希望它有效。

  • 如果您还没有测试过,请不要发帖。 (3认同)