Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  165] [ 1]  / answers: 1 / hits: 21353  / 13 Years ago, tue, august 23, 2011, 12:00:00

I have a handler for onbeforeunload



window.onbeforeunload = unloadMess;
function unloadMess(){
var conf = confirm(Wait! Before you go, please share your stories or experiences on the message forum.);
if(conf){
window.location.href = http://www.domain.com/message-forum;
}
}


but I'm not sure how to know if the url they clicked on the page is within the site.



I just want them to alert them if they will leave the site.


More From » jquery

 Answers
45

It's not possible to do this 100% reliably, but if you detect when the user has clicked on a link on your page, you could use that as a mostly-correct signal. Something like this:



window.localLinkClicked = false;

$(a).live(click, function() {
var url = $(this).attr(href);

// check if the link is relative or to your domain
if (! /^https?://./.test(url) || /https?://yourdomain.com/.test(url)) {
window.localLinkClicked = true;
}
});

window.onbeforeunload = function() {
if (window.localLinkClicked) {
// do stuff
} else {
// don't
}
}

[#90469] Monday, August 22, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
carolinabritneyp

Total Points: 75
Total Questions: 102
Total Answers: 105

Location: Armenia
Member since Fri, Apr 16, 2021
3 Years ago
;