Skip to content
the poor coder

how to restrict page leave in javascript

Poor Man
Published
Reading time
2 min read
On this page

How to Restrict Page Leave in JavaScript#

Have you ever been in a situation where you want to stop the user from leaving your web page accidentally? Well, it's possible with JavaScript. In this article, I will show you how to restrict page leave in JavaScript.

Method 1: Using the onbeforeunload Event#

The onbeforeunload event gets triggered when a user tries to leave a web page. Therefore, we can use it to restrict users from leaving the page accidentally.

Text

window.onbeforeunload = function() {
  return "Do you really want to leave?";
};

The above code will trigger an alert message when the user tries to leave the page. However, this method has a significant drawback - it will trigger the alert message even if the user wants to close the window or reload the page. Therefore, we need a more refined solution.

Method 2: Using the addEventListener Method#

We can use the addEventListener method to add an event listener to the window object. The listener will listen for the beforeunload event and trigger the alert message only if certain conditions are met.

Text

window.addEventListener('beforeunload', function (e) {
  if (someCondition) {
    // Cancel the event
    e.preventDefault();
    // Chrome requires returnValue to be set
    e.returnValue = '';
  }
});

In the above code, we added a condition that needs to be met before triggering the alert message. If the condition is not met, the event will be canceled, and the user will be allowed to leave the web page freely.

Method 3: Using the Confirm Dialog Box#

Another way to restrict page leave is by using the confirm dialog box. The confirm dialog box is a built-in JavaScript dialog box that prompts the user to confirm or cancel an action.

Text

window.onbeforeunload = function() {
  return confirm("Do you really want to leave?");
};

The above code will trigger the confirm dialog box when the user tries to leave the page. If the user clicks the "OK" button, it will allow the user to leave the page. However, if the user clicks the "Cancel" button, it will prevent the user from leaving the page.

These are some of the ways to restrict page leave in JavaScript.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC