我有一个密码保护链接,该链接将在新选项卡中打开,但我现在希望该标签在一段时间后超时。我试过这个但无济于事,任何帮助都会很棒!
<script>
var newWin;
function validatePass(){
if(document.getElementById('password').value == 'Techsoc'){
newWin = window.open('https://docs.google.com/spreadsheets/d/e/2PACX-1vRsNGb741vTBtSGlzbxnTsoZpFMs0l9ZMrD54uE4XGy6FvSMmNfp3RPWvDU3vI32iSIp07CEshGLo0M/pubhtml');
}else{
alert('Incorrect Password, please try again.');
return false;
setTimeout(function(){newWin.close()}, 5000);
}
}
</script>
答案 0 :(得分:1)
问题在于,当在if中创建新窗口时,超时在else语句中。现在,如果您输入密码并单击按钮,则应打开新选项卡,并在超时设置的时间后关闭。
<input type="password" id="password"></input>
<button OnClick="validatePass()">Log In</button>
<script>
var newWin;
function validatePass() {
if (document.getElementById('password').value == 'Techsoc') {
newWin = window.open('https://docs.google.com/spreadsheets/d/e/2PACX-1vRsNGb741vTBtSGlzbxnTsoZpFMs0l9ZMrD54uE4XGy6FvSMmNfp3RPWvDU3vI32iSIp07CEshGLo0M/pubhtml');
setTimeout(
function () {
newWin.close()
}, 5000);
}
else {
alert('Incorrect Password, please try again.');
return false;
}
}
</script>
&#13;