jQuery not closing window
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.
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 sec开发者_如何学Goonds.<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";
?>
If you wish to see this running live you can try it out here.
window.close @ MDN (Mozilla docs)
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 agrees :
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).
try getting reference to your window. if you used window.open(), try this:
var wind = window.open(params);// then close..
wind.close();
I tried your code in Google Chrome and is working.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function()
{
//wait a few seconds and close the window
setInterval(function()
{
window.close();
}, 5000);
});
</script>
I have read that in IE also works. However, maybe you're using FireFox to test your page, and that's why is not working. Probably you can search for a solution targeting FireFox.
UPDATE: I have tested another solution which works. However, it needs higher privileges:
$(document).ready(function()
{
function closeWindow() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
alert("This will close the window");
window.open('','_self');
window.close();
}
//wait a few seconds and close the window
setInterval(function()
{
window.close();
closeWindow();
}, 5000);
});
The result is an alert message asking the user about the level of privileges. Probably is not the best way to go. Maybe, instead of closing a window, you can redirect the user to something relevant.
精彩评论