Welcome to WebmasterWorld Guest from

Forum Moderators: open

onbeforeunload help!

5:11 pm on Apr 1, 2009 (gmt 0)

5+ Year Member

i am new to javascript, so thanks in advance. i've created a form for a user to complete, but i want to warn them if they leave the particular page without completing the form. i've added onbeforeunload, but the warning appears even when the page is refreshed, which is not what i want.

is there any way to check in javascript whether the next requested page equals the current page (i.e. page refresh or a call to the same page)? location.href == ?


5:46 pm on Apr 1, 2009 (gmt 0)

5+ Year Member

figured it out! i've added "onclick" to the button/link used when i do not want a warning to pop up. onclick calls a javascript function that sets a variable to true. only when that variable is false will the warning pop up.
7:02 pm on Apr 1, 2009 (gmt 0)

5+ Year Member

another question... i have a page called contest.php that consists of 3 different "steps" of the competition, so essentially the page is recalled 3 times before the person has successfully entered the competition. if a person clicks away from this page, then they will receive the warning message. i would like to track how many people leave the contest prior to completing all the steps. can i do this with onunload by inserting into a database when the page is unloaded? if so, how would i insert into a mysql database onunload?


7:57 pm on Apr 1, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

You can't use onunload like that. An alternative would be to track how many people hit each step. For example, user reaches first step, database is updated, user reaches second step, database is updated... etc. You can then track how many people reached the first step, up to n steps, and subtract from the n step to determine how many people quit and at which step.

Featured Threads

Hot Threads This Week

Hot Threads This Month