In the code below I use the jQuery ajax() function to do a longpolling call. While the ajax function is running, I click on the link to download Firefox (for example) and the ajax call stops.
It actually acts like the call was succesful, but it wasn’t, it simply stopped running.
Why is that? I want to function to continue…
I have seen this problem in Firefox and Chrome. I haven’t tested other browsers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function longPoller() {
$.ajax({
type: "GET",
url: '/long-loading-page.php', /* <?php sleep(5000); ?> */
dataType: 'json',
async: true,
cache: false,
timeout:50000,
success: function(data){
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('error');
},
complete: function() {
alert('complete');
}
});
}
$(document).ready(function(){
setTimeout(function(){ longPoller() }, 1000); //start the longPoller() function
});
</script>
</head>
<body>
<a href="http://download.mozilla.org/?product=firefox-3.6.9&os=win&lang=en-US">download Firefox! (clicking this link will stop the jQuery ajax call... why?)</a>
</body>
</html>
When clicking the download link you are leaving the page, even it does not look so. If there would no file transfer, you would see the requested page.. try to set a
target="_blank"or use an iframe as target for the link.