<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
<script>
var win;
$(document).on('click', '#okGoogle', function (event) {
event.preventDefault();
url = "http://www.google.com/";
if (!win) {
win = window.open(url, '_blank');
}
else {
win.focus();
}
});
</script>
</head>
<body>
<button id="okGoogle">Ok Google</button>
</body>
</html>
此代码工作正常。但是当新打开的窗口关闭时,代码不会再次打开它。有什么建议吗?
答案 0 :(得分:2)
您只需通过其closed
property检查窗口是否已关闭。
if (!win || win.closed) {
win = window.open(url, '_blank');
}
在这里看到工作小提琴 https://jsfiddle.net/c7q54ewt/