我正在为电子商务网站制作一个表单,供用户请求有关项目的PDF.表单工作正常,并将用户发送到"谢谢"页面.在感谢页面上,我让jQuery在5秒后设置了关闭窗口的间隔.但它不起作用,也没有在一个链接中嵌入close命令.但是,如果"谢谢"页面在新选项卡中打开,则间隔将起作用.
这是php文件中的代码(在表单成功提交后调用).
/* Results rendered as HTML */
$theResults = <<<EOD
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Thanks</title>
<script type="text/javascript" src="css/00000001/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
//wait a few seconds and close the window
setInterval(function()
{
window.close();
}, 5000);
});
</script>
<style type="text/css">
body{
background-color: transparent;
margin: auto auto auto auto;
border: none;
}
#general-copy{
background-color: transparent;
margin: -5px auto auto 0px;
position: absolute;
width: 300px;
height: 350px;
padding: 0px 15px 15px 15px;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 14px;
font-style: normal;
font-variant:normal;
font-weight:100;
text-align: justify;
z-index: 3;
}
</style>
</head>
<body><br /><br />
<div class="lettertext">
Thank you for your request.<br />
We will email you a pdf all about $item2Field.
<br><br>
<p style="font-size: 10px;">This window should close itself in 5 seconds.<br>
If it doesn't please click <a href="javascript:window.close()">here</a> to return to the site.
</p>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>
Run Code Online (Sandbox Code Playgroud)
如果你希望看到这个现场直播,你可以在这里试试.
window.close @ MDN(Mozilla docs)
仅允许对使用window.open方法由脚本打开的窗口调用此方法.如果脚本未打开窗口,则JavaScript控制台中将显示以下错误:脚本可能无法关闭脚本未打开的窗口.
如果相应的浏览上下文A是由脚本创建的辅助浏览上下文(而不是用户的操作),则Window对象上的close()方法应该是,并且如果调用的脚本的浏览上下文是允许方法导航浏览上下文A,关闭浏览上下文A(并且也可以丢弃它).