0

I want to show alert when user is leaving our website within same tab of the browser. I searched and found unload event, which triggers each time both when user is leaving or clicking our website link.

For example I'm at abc.com I must not got alert box when I'm clicking my site links. But I must got alert box when I'm writing xyz.com in address bar.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ali Mehdi
  • 884
  • 1
  • 11
  • 33
  • http://stackoverflow.com/a/7080331/4543256 – Rudie Visser Nov 24 '15 at 10:19
  • The `onbeforeunload` event fires when leaving the current page, however what you require (that you can detect if the user is leaving your site through it) is not possible, as it will fire for every page regardless of destination. – Rory McCrossan Nov 24 '15 at 10:20
  • I have gone through this post... it is not telling whether i am leaving to different domain – Ali Mehdi Nov 24 '15 at 10:20
  • 1
    You can check if user clicked anchor redirecting to other domain but you cannot check if he navigates using e.g address bar to navigate to other domain – A. Wolff Nov 24 '15 at 10:22
  • Possible duplicate http://stackoverflow.com/questions/821011/how-do-you-prevent-javascript-page-from-navigating-away – John R Nov 24 '15 at 10:23
  • @AliMehdi what about this one http://stackoverflow.com/a/147765/4543256 – Rudie Visser Nov 24 '15 at 10:23
  • @JohnR That's not a dupe. Question here is different – A. Wolff Nov 24 '15 at 10:23
  • Please find my edit question i elaborated more – Ali Mehdi Nov 24 '15 at 10:27
  • @AliMehdi Part of the problem: http://stackoverflow.com/questions/18824154/how-do-i-block-the-window-onbeforeunload-event-for-a-tag/18824483#18824483 Now still unclear what you mean here: `But i must got alert box when i am writting xyz.com in alert box`, writing in alert box??? – A. Wolff Nov 24 '15 at 10:28
  • I have to agree with @A.Wolff, the question is very unclear. – Rudie Visser Nov 24 '15 at 10:30
  • 1
    Possible duplicate of [javascript before leaving the page](http://stackoverflow.com/questions/7080269/javascript-before-leaving-the-page) – AlexVogel Nov 24 '15 at 10:30
  • No no... i edited... writing in address bar – Ali Mehdi Nov 24 '15 at 10:30
  • So that's a dupe of: http://stackoverflow.com/questions/18824154/how-do-i-block-the-window-onbeforeunload-event-for-a-tag/18824483#18824483 ??? – A. Wolff Nov 24 '15 at 10:33
  • No it is not dupe of what i am asking right now – Ali Mehdi Nov 24 '15 at 10:36
  • @AliMehdi So exactly how is it different? Not checking for same domain or what? – A. Wolff Nov 24 '15 at 10:37
  • This questions tells about working with anchors... but what about If I manually reloads the same website.. Your indicated question just gives a part of solution... What about form submission and other things – Ali Mehdi Nov 24 '15 at 10:41

2 Answers2

2

I see your question now, answer is NO, you can not do it, you can do whatever on your page in your domain, you have full control of it, but you can't not detect you are going to a different domain, just not possible, that the browser's behavior, and if the other site does not have your script, you have lost control on the browser.

For example, any time if the browser loads your script, you have all control, once it went to a different site, browser unload your script, what you can do? But sure, if you have scripts loaded, you can always check what is your current path/domain.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kevin Simple
  • 1,225
  • 10
  • 22
0

You can indeed use the unload event. The thing missing is navigating away when a user clicks on your own links.

This can be achieved by adding a click handler on all your links which sets a global flag (e.g. userClicked=true) which can be used in the unload handler

Example (jsFiddle):

var userClicked = false;
$( "body" ).delegate( "a", "click", function() {
    userClicked = true;
});

window.onbeforeunload = function() {
    if (!userClicked) {
        return "Your message";
    }
}
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77