如何防止在新选项卡中打开iframe中的链接 [英] How to prevent links in iframe from opening in new tab

查看:85
本文介绍了如何防止在新选项卡中打开iframe中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我制作的基于网络的操作系统制作了一个基于网络的小型web浏览器。我注意到,在一些网站中,他们有喜欢在新标签中打开的链接。有没有一种方法可以防止这种情况发生,并且在iframe中打开链接?

以下是我为整个浏览器编写的代码:

 < html> 
< head>

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

//检查用户是否输入http://或不是
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 =submitvalue =Help>< / form>
< / div>

地址:
< div id =logo>
< img src =stallion.png>
< / div>
< input type =textstyle =width:400px; name =urlid =url>
< input type =buttonvalue =Goid =load>

< div>
< input type =imagesrc =back.pngheight = 25 width = 25 onclick =back()>
< input type =imagesrc =forward.pngheight = 25 width = 25 onclick =forward()>
< input type =imagesrc =refresh.pngheight = 26 width = 26 onclick =refresh()>
< / div>

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

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

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

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

< / html>


解决方案

添加 target =_ self

 < iframe src =http://google.comtarget =_ self >< / iframe中> 

目标属性为:


_self =在被点击的同一框架中打开链接文档(这是默认设置)



_blank =在新窗口中打开链接文档或



_parent =在父框架中打开链接文档
$ b

framename =在命名框架中打开链接的文档


来自以下方面的信息:



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


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.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>

解决方案

Add 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

Information from:

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

这篇关于如何防止在新选项卡中打开iframe中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆