Pass an alert upon exiting specific domain path - javascript

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.

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
});

Related

get new url beforeunload to cancel specified links [duplicate]

I've searched for hours, but I couldn't find a solution for this.
window.onbeforeunload = warn;
This doesn't work:
function warn (e)
{
var destination = e.href;
alert(destination );
}
Okay, so to clear the things. If the user clicks on a link on the page itself, it is easy, because you can add an eventhandler to all of the links onclick event, but. I want to catch the address, what the user types into the url box of the browser.
Because it can't be done. The new location is private/sensitive information.
Nobody wants you to know which sites they visit when they leave your site.
If you just want to see what link destination, you can use :
document.activeElement.href
But getting the address line destination is not possible.
I've heard of solutions where they fire off an event if the mouse moves up to the address line (to warn the user that there are unfinished processes that have not been dealt with), but this sort of hack I would never do.
Kaze's answer is an interesting approach, but looking at the element focus when the page is navigated away from isn't really reliable. Partly because there is a delay between the link click and the navigation away from the page (during which time the user may move focus to some other element, but also because a link may be focused (eg. by keyboard control, or mousedown-without-click) without actually being used to navigate away from the page. So if you focused a link then closed the window, it'd think you were following the link.
Trapping onclick for every link on the page (plus onsubmit on every form) is slightly more reliable, but can still be fooled due to the delay. For example you click a link, but then before the new page starts loading hit the back button (or press Escape). Again, if you close the window it thinks you're following the link.
I want to catch the address, what the user types into the url box of the browser.
There is no way that will ever happen. It is an obvious privacy no-no.

Detecting a user leaving my website [duplicate]

I've searched for hours, but I couldn't find a solution for this.
window.onbeforeunload = warn;
This doesn't work:
function warn (e)
{
var destination = e.href;
alert(destination );
}
Okay, so to clear the things. If the user clicks on a link on the page itself, it is easy, because you can add an eventhandler to all of the links onclick event, but. I want to catch the address, what the user types into the url box of the browser.
Because it can't be done. The new location is private/sensitive information.
Nobody wants you to know which sites they visit when they leave your site.
If you just want to see what link destination, you can use :
document.activeElement.href
But getting the address line destination is not possible.
I've heard of solutions where they fire off an event if the mouse moves up to the address line (to warn the user that there are unfinished processes that have not been dealt with), but this sort of hack I would never do.
Kaze's answer is an interesting approach, but looking at the element focus when the page is navigated away from isn't really reliable. Partly because there is a delay between the link click and the navigation away from the page (during which time the user may move focus to some other element, but also because a link may be focused (eg. by keyboard control, or mousedown-without-click) without actually being used to navigate away from the page. So if you focused a link then closed the window, it'd think you were following the link.
Trapping onclick for every link on the page (plus onsubmit on every form) is slightly more reliable, but can still be fooled due to the delay. For example you click a link, but then before the new page starts loading hit the back button (or press Escape). Again, if you close the window it thinks you're following the link.
I want to catch the address, what the user types into the url box of the browser.
There is no way that will ever happen. It is an obvious privacy no-no.

stop override all JavaScript redirect .. only allow a click

I have issue with mobile website.. which are I cant track-down, it may be from malware on user phone or maybe adnetwork.. only few visitor got this issue
when user visit my web.. they will redirect to other website mobiphone-tips or cellphone-tips and it random very few time days, normally in fist time of day only.
I'm trying looking the way to override all default redirect javascipt to return false, so keep user stay on page.. they only go as they wish when click on a links
i have tried with window.onbeforeunload place on header .. but this also stop user from click a link
Please help

Don't allow user to close window for incomplete form

I have a big form on jsp page where user enters some information.
My requirement is that if user move away to another link or close window, I should alert that "Your form changes will be lost if you move away from screen, please press save before moving away from this screen." If user press ok, we should navigate to next page else stay on same page.
How can I achieve this? Any javascript method available for this? Any help much appreciated.

Use Javascript Alert To Keep Users On A Page

I am creating an internal web based application that will not be the target audience of the web.
I understand the frustration of alert boxes and forcing people to do certain things.
With that said, what I am attempting to do is create a javascript function, that unless a user clicks a link on a specific page, if they try to navigate away from the page other than using a link on the page, I would like to alert them and say, sorry you need to click the appropriate link to exit.
What my issue is, is that I need to lock out fields, and what I can do when a user hits an edit page, im going to write to a table that user to the lockoutuser colum. If a value exist, that user can access the record if it is null, it means no one is editing the record. If someone clicks to go into that record they lock it out, my means of updating the lockoutuser colum could be ajaxy on unload of the page, but the page could be unloaded for 2 reasons, 1 the edit form is submitted or the user leaves the page.
An alert that would say, sorry you can leave this record without clicking the big red button that says unlock, and force the user without refreshing to stay on the page.
I understand the machine could crash and or an alt f4 or a brute end task on the browser will still leave me other work to unlock the record
You need to use the onunload event of the page to present a messagebox when the user tries to leave your page. Check out this example: http://www.codetoad.com/javascript/miscellaneous/onunload_event_eg.asp

Categories

Resources