tony noriega
tony noriega

Reputation: 7673

Pass an alert upon exiting specific domain path

I have been tasked with the following.

Display an alert "You are now leaving the special section of our website" when a visitor leaves a specific directory of our site.

For example.

Visitor comes to our home page www.sample.com.

Clicks link to navigate to "www.sample.com/special-place/"

Clicks another link and navigates to "www.sample.com/no-special-place/"

Anytime the user leaves that section of our site, the message needs to appear.

And no, we can not just show this once (cookie based) it needs to display every time they leave that section of the site. Yes. I know. Horrible user experience.

Could this be accomplished with Javascript by capturing the visitor when they enter the special-place path, then exiting to any other path that does not have the matching characters of "/special-place/"?

Possibly using jQuery to do this?

Any insight or thoughts are greatly appreciated.

Upvotes: 0

Views: 81

Answers (1)

megawac
megawac

Reputation: 11363

Use window.onbeforeunload which will fire a prompt asking the user if they want to leave.

$(window).bind("beforeunload", function() {
   //logic if you want to show prompt
   return "Are you sure you want to leave";//this text will be shown in a prompt
});

Upvotes: 4

Related Questions