+ 2
How to trigger a JavaScript event after chrome browser tab close button is clicked?
I want to create a page with JavaScript..after page created it has contents. I would like to load this page in chrome. after the page loaded, if user would like close the current page, user try to click on the current tab's close button. when this button is triggered, I would like to perform an event in JavaScript..any one provide me a solution for this?
10 Réponses
+ 5
The onunload event support is browser dependent, some browsers allow custom message, others show no message at all. Look at the following links for details of why some browsers dropped support for that event:
https://bugs.chromium.org/p/chromium/issues/detail?id=587940
And see what MDN says about browser compatibility:
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload
Hth, cmiiw
+ 3
window.onunload = function (){
//do stuff
}
find more:
https://www.sololearn.com/learn/JavaScript/2758/
+ 3
My JS is a little rusty so correct me if I'm wrong. JS runs in the browser in the open window. So if you close the window the JS also stops because you closed the window. Therefore you can't do more JS after the window has been closed.
+ 2
@thirus did you noticed the keyword you used to call the unload event,, check your code
hai ..please see my code given below for above question
👇
window.unload=function () {
var result=confirm ("whether you want to leave this page");
if(result) {
alert("Thank you for visiting");
} else {
alert("Thank you for staying with us");
}
}
it's not 👉window.unload, but
window.onunload👈 check it very well, and if it still didn't work then follow #Ipang post
+ 1
hai above code not working..still looking for a answer
+ 1
then try the link to find more ,,
https://www.sololearn.com/learn/JavaScript/2758/
but I suggest posting the link to your code their by allowing us to view your code and explore the bugs
+ 1
hai ..please see my code given below for above question
window.unload=function () {
var result=confirm ("whether you want to leave this page");
if(result) {
alert("Thank you for visiting");
} else {
alert("Thank you for staying with us");
}
}
I have tried the same with window.onbeforeunload and window.blur ..but none of them was worked
my expectations is" I will load an web page. after sometimes , if the user tries to close the current tab of Google Chrome, I want to ask above confirmation page".
could anyone tell me, how to achieve my expectation in pure JavaScript?
Am I did anything wrong?
+ 1
hai @Ben. is this possible to show a confirmation box, before the window has been closed?
+ 1
thanks @th3_c0d3r
0
U ar welcome 😀