• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Using onbeforeunload event of Javascript

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have been using the following script to ask the user if they wanted to close the window.

<script language="JavaScript" event="onbeforeunload()" for="window">
event.returnValue = "Are you sure you want to close this application ? You have unsaved data and closing this application will cause any unsaved data to be lost. Do you wish to continue closing the application ?";
</script>

This message is triggered whenever the page unloads. But I want to display the above message only when the user clicks the X mark in the window. Is there any way through which i can achieve this?

I tried to find out if the user has clicked the X mark by getting the coordinate of the document using the following code .

<script language="JavaScript" event="onbeforeunload()" for="window">
var coordX = event.clientX + document.body.scrollLeft;
var coordY = event.clientY + document.body.scrollTop;
if ((coordX > 770 && coordY < -110){
event.returnValue = "Are you sure you want to
close this application ? You have unsaved data and closing this application will cause any unsaved data to be lost. Do you wish to continue closing the application ?";}
</script>

But the problem is it doesnt work for all resolutions. Also,When a list box grows up to the title bar, then selecting any value from the list box triggers this message.

Can anyone suggest me some idea ?
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The detect cursor position X hack only works in one browser and it does not account for alt f4, ctrl-w, and file-quit to name a few.

Also, there are better ways of adding script to the page that using events on script tags.

The best way to code for unbeforeunload is to not show it based on events that you want to happen. You set a flag submission on links, the form submit, and so on.

Eric
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic