Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Browser's stop button event

Tags:

jquery

Is there a way to detect when the user has clicked the stop button on any browser? I want to stop the script from running when the user decides to click the stop button when uploading a file, so I can trigger the script to refresh the page.

like image 793
user1105430 Avatar asked Apr 22 '26 02:04

user1105430


1 Answers

Unfortunately the browser's stop button doesn't affect AJAX requests. If you want to cancel an AJAX request you can abort it like below:

var xhr = $.ajax(
    url: "http://someurl.org",
    success: function() { ... }
);

xhr.abort();

The button to trigger the abort will need to be in your HTML since the browser's stop button cannot abort the request. If the user went to another page the browser would stop listening though and this is essentially what happens when you call xhr.abort().

like image 76
John Culviner Avatar answered Apr 26 '26 00:04

John Culviner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!