1

As the title says ....

Sorry that some ppl got question wrong...

I want to show confirmation on these cases only:

1.Browser window is closed.

2.There is a redirect to different domain

thanks

eugeneK
  • 10,750
  • 19
  • 66
  • 101
  • Duplicate: http://stackoverflow.com/questions/803887/can-i-pop-up-a-confirmation-dialog-when-the-user-is-closing-the-window-in-safari – Ramiz Uddin Nov 15 '10 at 09:57
  • I assume when you say "redirect" you just mean the link the user clicked on points to a different domain ? "redirect" normally means something very specific in web development, i.e. a server side redirect. – andynormancx Nov 15 '10 at 10:20

3 Answers3

1

By using the onbeforeunload event:

window.onbeforeunload = function() { 
    return 'Are you sure you want to navigate away from this page' ;
};
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thanks. I've seen that answer but it doesn't check whether page i want to redirect to is from the same domain... – eugeneK Nov 15 '10 at 09:31
  • 1
    @eugeneK, I am afraid there's no reliable way of achieving this. – Darin Dimitrov Nov 15 '10 at 09:37
  • is there a way that can cover at least some cases? – eugeneK Nov 15 '10 at 09:39
  • @eugeneK, I am sorry, the target url the user is navigating to is a private/sensitive information which is not exposed to client scripts. If you are using hyperlinks you can of course use some javascript to *enhance* those links and check the target url when clicked but not in a `onbeforeunload` event. – Darin Dimitrov Nov 15 '10 at 09:42
1

The first question has already been very well covered.

Alerts when navigating away from a web page

The second part is more troublesome. You won't get anything in the onbeforeunload event that tells you which link was clicked on to trigger it.

If you wanted to detect which link caused the close you'd have to attach some script to each link's onclick event. You'd also have to remove the href from the link and store it somewhere, you could do it like this using jQuery:

<script>
$(window).load(
    function () {
        $("a").each(function () {
            $(this).data("url", $(this).attr("href")).attr("href", "#").click(
                function () {
                    var destination = $(this).data("url");
                    // do something to check the domain, I'm just checking the whole url
                    if (destination == "http://norman.cx/photos/") {
                        alert("Not navigating to: " + destination);
                    } else {
                        alert("Navigating to: " + destination);
                        window.location.href = destination;                         
                    }
                    return false;
                }
            );
        });
    }
)           
</script>
<a href="http://norman.cx/photos/">test</a>
<a href="http://microsoft.com/">msft</a>

This gets the href for each link, stores it against the link using data() and sets the link's href to "#". It then attaches a click event to each link that retrieves the url from data() and does something with it.

You could do it the other way, conditionally change the href and add the click event only on links that where on another domain.

Whether that would be a good idea or not is another question. It is at the very least an unusual thing to find yourself doing.

Community
  • 1
  • 1
andynormancx
  • 13,421
  • 6
  • 36
  • 52
  • @andynormancx, it is not. There is a partial answer. I want to prevent from this confirm when next url is from the same domain as the previous one. – eugeneK Nov 15 '10 at 09:31
  • @andynormancx, thanks for changing mood and for the answer. I think you more or less right with your creative idea. Never thought of selecting all a's on DOM and checking their urls against my domain. While address bar or x exit click will be excluded. Is "return false" as the end of your function closes "onbeforeunload"? – eugeneK Nov 16 '10 at 07:40
  • "return false" is there to stop the browser moving the page when the user clicks in the link http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0 I instinctively add it to any click event... – andynormancx Nov 16 '10 at 12:16
  • and as to changing mood, I'm afraid your initial question was still unclear, you need to be more careful in putting together your questions – andynormancx Nov 16 '10 at 12:19
  • @andynormancx, if user changed url in address bar or clicked on close window/tab your function won't work, right? So how do i create case for two of these? – eugeneK Nov 17 '10 at 08:18
  • Correct. I'm afraid there really is no solution that I am aware of for the change url/close window cases. You just aren't given the information you need in the events to achieve it. – andynormancx Nov 17 '10 at 09:58
-1
<body onunload="doYourThing();" >
MatuDuke
  • 4,997
  • 1
  • 21
  • 26