javascript - How to prevent links in iframe from opening in new tab - Stack Overflow

admin2025-04-21  0

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?

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=".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="/" 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>

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?

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./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./" 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>
Share Improve this question edited Mar 21, 2015 at 21:07 UltraStallion asked Mar 21, 2015 at 21:02 UltraStallionUltraStallion 1311 gold badge3 silver badges11 bronze badges 1
  • I think that's not possible. – Iulian Onofrei Commented Mar 21, 2015 at 22:03
Add a ment  | 

2 Answers 2

Reset to default 1

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"

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.

I have provided a jsFiddle below

https://jsfiddle/L5dhp80e/

Html

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

<br />
<a class="removeBlank" href="http://www.google." 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;
    })
});

However if the iframe content is cross domain I don't think you can edit any of the code at all.

Get DOM content of cross-domain iframe

Add target="_self"

<iframe src="http://google." 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

Information from:

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745177013a288970.html

最新回复(0)