Welcome Guest ( Log In | Register )

Bump Topic Topic Closed RSS Feed

Outline · [ Standard ] · Linear+

JavaScript Javascript detect close and refresh browser, need help from javascript sifus

views
     
TSkoayty
post Nov 24 2008, 11:03 AM, updated 16y ago

Getting Started
**
Junior Member
182 posts

Joined: Apr 2007
I have researched for a few days and couldn't get a perfect solution for javascript to detect every close and refresh action on a browser(IE only)

for close, i manage to detect: ALT+F4 key press, [X] button clicked key (but to a certain extent when the browser window is maximized only, but will sometimes be mixed up with refresh button click)
i failed to detect browser's File>Exit context menu command, and taskbar's right click>Close context menu command

for refresh, i managed to detect: CTRL+R and F5 key press,
but failed to detect browser's refresh button click event(due to the flexibility of the navigation toolbar customization ability), and context menu click event, both from View>Refresh and right click on page>refresh, so anyone could guide me to detect those actions that i have failed would be greatly appreciated.

This post has been edited by koayty: Nov 24 2008, 12:01 PM
thehobo
post Nov 24 2008, 11:49 AM

Getting Started
**
Junior Member
74 posts

Joined: Aug 2006


QUOTE(koayty @ Nov 24 2008, 11:03 AM)
I have researched for a few days and couldn't get a perfect solution for javascript to detect every close and refresh action on a browser(IE only)

for close, i manage to detect: ALT+F4 key press, [X] button clicked key (but to a certain extent when the browser window is maximized only)
i failed to detect browser's File>Exit context menu command, and taskbar's right click>Close context menu command

for refresh, i managed to detect: CTRL+R and F5 key press,
but failed to detect browser's refresh button click event(due to the flexibility of the navigation toolbar customization ability), and context menu click event, both from View>Refresh and right click on page>refresh, so anyone could guide me to detect those actions that i have failed would be greatly appreciated.
*
ur so screwed... another way is to write a plugin that directly interacts with Windows API but then again why need such elaborate event detection?
TSkoayty
post Nov 24 2008, 12:00 PM

Getting Started
**
Junior Member
182 posts

Joined: Apr 2007
because i need to detect closing to remove login sensitive session information and log out the user, as it is critical to do so. no other choice.

This post has been edited by koayty: Nov 24 2008, 12:04 PM
thehobo
post Nov 24 2008, 01:15 PM

Getting Started
**
Junior Member
74 posts

Joined: Aug 2006


QUOTE(koayty @ Nov 24 2008, 12:00 PM)
because i need to detect closing to remove login sensitive session information and log out the user, as it is critical to do so. no other choice.
*
Why not do it another way.
1. AJAX it by programming the client app/page to ping your server every xx seconds on a page. Store the pingback in database
2. Do a round robin poll application that checks the active session pingback every y minutes. Sessions that doesn't receive ping back within xx seconds, you can safely logged them out and remove the session app.

But server load will increase lor.

EDIT: Omg! Spoken too soon. There is a cross browser solution here Browser Refresh Event

This post has been edited by thehobo: Nov 24 2008, 02:18 PM
winmxbb
post Nov 24 2008, 01:58 PM

On my way
****
Junior Member
512 posts

Joined: Jan 2008
QUOTE(koayty @ Nov 24 2008, 11:03 AM)
I have researched for a few days and couldn't get a perfect solution for javascript to detect every close and refresh action on a browser(IE only)

for close, i manage to detect: ALT+F4 key press, [X] button clicked key (but to a certain extent when the browser window is maximized only, but will sometimes be mixed up with refresh button click)
i failed to detect browser's File>Exit context menu command, and taskbar's right click>Close context menu command

for refresh, i managed to detect: CTRL+R and F5 key press,
but failed to detect browser's refresh button click event(due to the flexibility of the navigation toolbar customization ability), and context menu click event, both from View>Refresh and right click on page>refresh, so anyone could guide me to detect those actions that i have failed would be greatly appreciated.
*
CODE

<script type="text/javascript">    
window.onbeforeunload =
 function(){
    if((window.event.clientX<0) || (window.event.clientY<0)){ //if close window
   f1();
      event.returnValue = "Your changes will not be saved.";      
   }
   else { //if navigate to other page
    f1();
    event.returnValue = "Your changes will not be saved!";
   }
 }

 function f1(){
alert("this is function 1");

 }
</script>

<a href="http://www.yahoo.com">go to another link</a>


This post has been edited by winmxbb: Nov 24 2008, 01:59 PM
TSkoayty
post Nov 24 2008, 03:05 PM

Getting Started
**
Junior Member
182 posts

Joined: Apr 2007
QUOTE(winmxbb @ Nov 24 2008, 01:58 PM)
CODE

<script type="text/javascript">    
window.onbeforeunload =
 function(){
    if((window.event.clientX<0) || (window.event.clientY<0)){ //if close window
   f1();
      event.returnValue = "Your changes will not be saved.";      
   }
   else { //if navigate to other page
    f1();
    event.returnValue = "Your changes will not be saved!";
   }
 }

 function f1(){
alert("this is function 1");

 }
</script>

<a href="http://www.yahoo.com">go to another link</a>

*
this i have already done and
CODE
if((window.event.clientX<0) || (window.event.clientY<0))

will run if you: click on refresh button or click on [X] button
not answering my questions, but thanks for your effort


Added on
QUOTE(thehobo @ Nov 24 2008, 01:15 PM)
Why not do it another way.
1. AJAX it by programming the client app/page to ping your server every xx seconds on a page. Store the pingback in database
2. Do a round robin poll application that checks the active session pingback every y minutes. Sessions that doesn't receive ping back within xx seconds, you can safely logged them out and remove the session app.

But server load will increase lor.

EDIT: Omg! Spoken too soon. There is a cross browser solution here Browser Refresh Event
*
nope, not recommended to do such a thing, as the performance is very important, it's a big system and database size may range up to 100-200 Gigabytes


Added on
The system's security is very important, it's a web based system, so thats why i need a browser close detection, so that i can clear all the private data from cookie and etc. no compromises can be made on server performances.

This post has been edited by koayty: Nov 24 2008, 03:18 PM
alanyuppie
post Nov 24 2008, 04:22 PM

Look at all my stars!!
*******
Senior Member
2,791 posts

Joined: Jul 2006
From: here


QUOTE(koayty @ Nov 24 2008, 04:05 PM)

The system's security is very important, it's a web based system, so thats why i need a browser close detection, so that i can clear all the private data from cookie and etc. no compromises can be made on server performances.
*
Easy. Just don't use cookies or any mechanisms that "writes" P&C data to the client PC. there are other ways to remember session data. If you meant to create highly secure web applications, then better not be stingy on the hardware part. Otherwise, its useless to have the most secure code and yet have it ran on "average" server with paltry RAM to store session data for huge number of users/ or limited processor power to take the load.


snow
post Nov 24 2008, 11:45 PM

Enthusiast
*****
Senior Member
785 posts

Joined: Jan 2003



You may use onunload.

But you cannot depend on browser's close button and refresh button to perform such action. Browser can be terminated via task manager and there might be internet connection problem.
TSkoayty
post Nov 25 2008, 09:43 AM

Getting Started
**
Junior Member
182 posts

Joined: Apr 2007
QUOTE(snow @ Nov 24 2008, 11:45 PM)
You may use onunload.

But you cannot depend on browser's close button and refresh button to perform such action. Browser can be terminated via task manager and there might be internet connection problem.
*
yes that is true but then i will need the server to monitor every connection to it and to kill a session when it doesn't responds


Added on November 25, 2008, 9:44 am
QUOTE(alanyuppie @ Nov 24 2008, 04:22 PM)
Easy. Just don't use cookies or any mechanisms that "writes" P&C data to the client PC. there are other ways to remember session data. If you meant to create highly secure web applications, then better not be stingy on the hardware part. Otherwise, its useless to have the most secure code and yet have it ran on "average" server with paltry RAM to store session data for huge number of users/ or limited processor power to take the load.
*
point noted thumbup.gif

This post has been edited by koayty: Nov 25 2008, 09:44 AM
shinnosuke
post Nov 25 2008, 04:50 PM

who are you?
******
Senior Member
1,997 posts

Joined: Jan 2003
From: Your Grandma With Kisses




QUOTE(koayty @ Nov 25 2008, 09:43 AM)
yes that is true but then i will need the server to monitor every connection to it and to kill a session when it doesn't responds
Normally we can set the session timeout in most webserver, set it to the smallest acceptable value that you can eg: 1 minute. All the user information will be gone before other people can grab it.
TSkoayty
post Nov 26 2008, 10:16 AM

Getting Started
**
Junior Member
182 posts

Joined: Apr 2007
QUOTE(shinnosuke @ Nov 25 2008, 04:50 PM)
Normally we can set the session timeout in most webserver, set it to the smallest acceptable value that you can eg: 1 minute. All the user information will be gone before other people can grab it.
*
nope, won't work, as the report generator may take a lot longer to generate report before the session times out, thanks for all the help, i'll have to find some way to solve this

Topic ClosedOptions
 

Change to:
| Lo-Fi Version
0.0159sec    0.27    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 03:20 PM