views:

60

answers:

1

It might sound silly, but I need this functionality.

Can I somehow cancel location.href = 'url' in javascript once it is executed?

for example on click of button i am changing current page with some resource intensive page, I want to give an option to user so that one can cancel it if next page is going to take long time to load.

Thanks and Regards

+1  A: 

Yes you can. You can use the window.onbeforeunload event to return an alert message that the user can confirm or cancel. When the user cancels it, the page navigation is cancelled.

jQuery example:

    window.onbeforeunload = function () {
        return "Are you really sure?";
    }

Please note that the alert message is displayed differently in various browsers.

Prutswonder
your example isn't a "jQuery example" - there's no jQuery wrapper there it's just pure JS and DOM.
Andy E
that is not jQuery example but Javascript. That example can be done without jQuery.
The Elite Gentleman
First and foremost: this does not work, does it? It should be `return confirm("Foobar");`
middus
Please see my comment no 3 in the question....
Software Enthusiastic