我们在开发过程中点击a标签或按钮跳转新页签时,如果再次点击该链接或按钮,还会重新打开一个页面。这样的交互效果不是很友好。
使用如下方式可以限制打开新的页签,只保留一个新页签。
1.使用target = ‘framename’
2.使用js逻辑判断
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<a href="http://www.baidu.com" target="framename" >百度</a>
<button onclick="openWindow('http://www.baidu.com')">百度</button>
</body>
<script>
var newWindow = null;
function openWindow(url){
if(newWindow == null){
newWindow = open(url, "_blank", "");
}else{
try{
newWindow.focus();
}catch(e){
newWindow = null;
openWindow(url);
}
}
}
</script>
</html>