+ 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?

2nd Mar 2018, 5:12 PM
Thiru Arasu
Thiru Arasu - avatar
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
7th Mar 2018, 3:41 AM
Ipang
+ 3
window.onunload = function (){ //do stuff } find more: https://www.sololearn.com/learn/JavaScript/2758/
2nd Mar 2018, 5:35 PM
Nura Programmer
Nura Programmer - avatar
+ 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.
7th Mar 2018, 1:42 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 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
7th Mar 2018, 7:26 AM
Nura Programmer
Nura Programmer - avatar
+ 1
hai above code not working..still looking for a answer
3rd Mar 2018, 8:01 AM
Thiru Arasu
Thiru Arasu - avatar
+ 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
3rd Mar 2018, 11:32 AM
Nura Programmer
Nura Programmer - avatar
+ 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?
3rd Mar 2018, 12:33 PM
Thiru Arasu
Thiru Arasu - avatar
+ 1
hai @Ben. is this possible to show a confirmation box, before the window has been closed?
7th Mar 2018, 2:33 AM
Thiru Arasu
Thiru Arasu - avatar
+ 1
thanks @th3_c0d3r
9th Mar 2018, 3:04 AM
Thiru Arasu
Thiru Arasu - avatar
0
U ar welcome 😀
9th Mar 2018, 3:19 AM
Nura Programmer
Nura Programmer - avatar