今天遇到了一个前端问题,就是在点击链接或按钮时跳转新页签时,如果再次点击该按钮,还会重新打开一个页面,这里我想要再次点击按钮时定位到第一次打开的页面,于是就有了下面的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<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>