jQuery不关闭窗口 [英] jQuery not closing window

查看:83
本文介绍了jQuery不关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为电子商务网站制作表格,供用户请求有关商品的PDF. 该表格可以正常工作,并将用户发送到谢谢"页面. 在感谢页面上,我让jQuery设置了5秒后关闭窗口的时间间隔. 但是它不起作用,也没有将close命令嵌入到链接中. 但是,如果在新标签页中打开感谢"页面,则间隔将起作用.

I'm making a form for an e-commerce site for the user to request a PDF about an item. The form works fine and sends the user to the "Thanks" page. On the thanks page I have jQuery set an interval to close the window after 5 seconds. But it does not work, neither does embedding the close command in a link. However, the interval will work if the "Thanks" page opens in a new tab.

这是php文件中的代码(在成功提交表单后调用).

Here is the code in the php file (gets called after form has been successfully submitted).

/* 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"; 


?>

如果您希望看到此实时运行,可以在此处尝试一下./p>

If you wish to see this running live you can try it out here.

推荐答案

window.close @ MDN(Mozilla文档)

window.close @ MDN (Mozilla docs)

仅允许使用此方法 要求打开的窗户 使用window.open方法的脚本. 如果窗口没有被打开 脚本中,出现以下错误 JavaScript控制台:脚本可能 关闭没有打开的窗户 通过脚本.

This method is only allowed to be called for windows that were opened by a script using the window.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.

W3C HTML5上的规范同意:

W3C norm on HTML5 agrees :

Window对象的close()方法 应该,如果相应的浏览 上下文A是辅助浏览 由脚本创建的上下文 (相对于 用户),以及 调用该方法的脚本是 允许浏览浏览 上下文A,关闭浏览上下文 A(并且可能也将其丢弃).

The close() method on Window objects should, if the corresponding browsing context A is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), and if the browsing context of the script that invokes the method is allowed to navigate the browsing context A, close the browsing context A (and may discard it too).

这篇关于jQuery不关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆