Click to See Complete Forum and Search --> : finding the browser action with javascript
kanakaprasad
10-07-2003, 03:00 PM
hi!
is there a way to know when the user is clicking back button and trying to close the bowser by clicking 'X' button.
is there any event for page when ur leaving for other page.
thanks in advance
Dimitri
10-07-2003, 06:36 PM
use the onunload event, for example, in your body tag (e.g. <body onunload="your code here">)
But be careful, the window document will probably no longer be accesible to your script when the js code in your onunload script runs, so avoid running js code that tries to reference objects in your page (as they'll likely no longer be available to it.)
Test it in both in NS and IE before you use it on your website.
Cheers.
Dimitri
kanakaprasad
10-08-2003, 07:24 AM
its works. thanks for the reply. now i am able to track the page moment.
one more simple question?
i have small functon that executes on onunload event and gives a confirm alert. how can i stop the page from going further if i say 'yes' in the alert. i tried with return false(); but not working thanks
kanakaprasad
10-08-2003, 11:52 AM
any suggestions folks! kinda urgent please !
Dimitri
10-08-2003, 01:23 PM
Returning true or false has nothing to do with the onunload event handler. That event is only used to run some js code... that's all. That event is not like onsubmit or confirm which accept a boolean.
So, no... you can't do anything about that (and that's *exactly* how it *should* be). Can you imagine the implications if someone could programatically deny a user the ability to close their own web browser? Yikes!
Can you be more specific as to WHY you want to do this? Perhaps we can figure out a better way to accomplish your intended goal. After all, your goal isn't to prevent the user from closing their web browser, just because you don't want them to leave your site, right? What's the actual problem you're trying to solve?
Dimitri
kanakaprasad
10-08-2003, 02:25 PM
well i appriciate your discussion. dear friend its like this. i have a data entry form and i dont want the user to loose any data thats entered, is lost by mistake (the action would be closing the browser, back button or other link on the form) i want to warn the user to know that there is some valid data on the form to be saved. would u like to save or leave to other page. hope i made my self clear. please let's find out the best way i would deal wiht it. its what user need so i have to accomplish some how.
thanks
Dimitri
10-08-2003, 02:46 PM
I was hoping that wasn't what you wanted to do (but suspected that it was. haha)
Once someone clicks that "X" button, that's pretty much it.
confirm() can only prevent the browser from closing momentarily, but it doesn't allow the user the ability to undo their decision (even if they want to).
So, what you may want to consider doing (depending on your level of coding ability) is to break up the form into several mini-forms on different pages. That way, the user can click a button called "next" (or something like that) and submit part of the form they've completed so far and go to the next page and fill out additional information.
This won't solve the problem, but at least if they close their browser they wouldn't have lost the data they'd already entered from the previous pages.
Another option (but also requires a certain level of expertise) is to submit the form fields they fill out (in the background), as they fill out the form.
This could be done with a java applet on the page that connects to your back-end and updates a database or something. Or, it could be done by saving the users responces in a persistent cookie (via javascript) which you can then use to repopulate the form with their data when they revisit the form (assuming they have javascript and persistent cookies enabled, and the responces don't exceed 4kb, total.)
Another way (which does not work in Netscape 4.x, but works in IE) is to read the form values in the page (by calling a function from the unonload event) and set those values to a cookie or perhaps open a new browser window that passes those files as parameters in the URL to a page on your site that can then store them in a DB or something.) This, also requires that you user doesn't have a popup killer app installed.
This wouldn't work in NS because the document object appears to be destroyed *prior* to the execution of the onunload even in NS (versus afterwards in IE, which is how it should be.)
There are lots of ways to go about it. None of them are simple however (as you can see), and they all require that you really know what you're doing as a programmer.
Sorry I couldn't be more helpful. Good luck.
Dimitri
kanakaprasad
10-08-2003, 02:50 PM
thanks alot friend. guess i have a clear mind now. probable i will more dipend on error handling than thinking on these ways. thanks once again
kanaka