Prity Kumari
Prity Kumari

Reputation: 85

Get newly requested url onbeforeunload event in javascript

I am running one application where i am checking page name in page load event. If the url found the page i am making one variable to true. Now when user will do anything on that page then in onbeforeunload event i am checking this variable is true, If so i am redirecting page to the previous page.

Now i want here if user visit some other page so that page url i can get in this event so based on that i can allow user to go to that page.

Is there any way to get the requested url in onbeforeunload event?

Upvotes: 0

Views: 1406

Answers (1)

Quentin
Quentin

Reputation: 943605

Not as such.

The user could be leaving the page by using a bookmark or typing a new address into the address bar. That is private information and you have no business reading it.

If the user is leaving the page by some action that your site triggered (such as clicking a link) then you could use JavaScript in response to the trigger (e.g. click event on the a element) to set a variable, then read that variable back in your onbeforeunload handler.

Upvotes: 1

Related Questions