Red
Red

Reputation: 6398

javascript - Prevent page loading

I want to prevent the page loading when "Editing" on m page occures.

so i got this code

window.onbeforeunload = function() {
  return "Are you sure you want to navigate away?";
}

Now i need to unbind this from the page.

is there any methods available ?

Thank you.

Upvotes: 1

Views: 5789

Answers (3)

ABHISHEK
ABHISHEK

Reputation: 47

You should use some flag to check if page is editing values or just navigating to other page

you can follow this thread too Javascript, controlling an onbeforeunload tag

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337714

To unbind the event, set it to null:

window.onbeforeunload = null;

It may however be better for program flow to put a condition inside your onbeforeunload handler, so that you have a single function running on page unload:

window.onbeforeunload = function() {
    if (myVar == "Editing") 
        return "Are you sure you want to navigate away?";
}

Upvotes: 1

Andrei G
Andrei G

Reputation: 1590

You set the handler to null:

window.onbeforeunload = null;

Upvotes: 2

Related Questions