jQuery 不关闭窗口

标签 jquery window

我正在为电子商务网站制作一个表单,供用户请求有关某个项目的 PDF。 该表单工作正常并将用户发送到“感谢”页面。 在感谢页面上,我让 jQuery 设置了 5 秒后关闭窗口的时间间隔。 但它不起作用,在链接中嵌入关闭命令也不起作用。 但是,如果“感谢”页面在新选项卡中打开,则该间隔将起作用。

这是 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"; 


?>

如果您想观看此直播,可以尝试一下 here .

最佳答案

window.close @MDN(Mozilla 文档)

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 norm on HTML5同意:

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 不关闭窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5149552/

相关文章:

python - 为什么 tkinter 无法识别该窗口正在调整大小?

python - 通过 pywinauto 调整记事本大小

javascript - 如何在 jQuery UI 布局插件中动态切换 `resizable` bool 值?

javascript - 使用 replaceWith() 方法通过 ID 和变量定位

javascript - 使用另一个属性代替输入值来更新总计

javascript - 第一次点击时日期选择器不显示

javascript - JSON 中的对象使用 $Each 循环,如何获取值

qt - 我可以使用opengl在窗口上画一些不属于我的东西吗?

c# - 拦截 windows mobile 上的最小化事件(紧凑型框架)

r - 如何在R中计算过去1个月的滚动平均值