No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Web development

Disable browser ‘Back’ button with Javascript

npn by npn
June 23, 2019
in Web development
Reading Time: 2 mins read
0
0
SHARES
3.8k
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 Introduction
    • 1.1 READ ALSO
    • 1.2 What is the location of the MySQL databases?
    • 1.3 Top 10 Best WordPress SEO themes of 2022
  • 2 Summary
Rate this post

Introduction

While implementing a business application in a certain case the other day, it was implemented because it was implemented with the requirement that “Do not allow browser to return” Leave it to I checked a couple of ways to deal with this problem and I found about 2 patterns … was not implement the business applications in the projects that the other day, you leave on record since the implementation have gotten the requirement that “I want that you do not go back in the browser”.

READ ALSO

What is the location of the MySQL databases?

What is the location of the MySQL databases?

April 24, 2022
590
Top 10 Best WordPress SEO themes of 2022

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
481

I checked out a couple of patterns to find out how to handle this problem.

One way is to go back to the original page immediately when you go back to the history by pressing the back button.

But this is not good. Make sure the user’s requirements can not be returned by the browser in the first place.

The second way to achieve this is realistic.

It is a method of deleting the current history when transitioning the page.

We will introduce this method this time.

Let’s conclude by disabling the browser’s “Back” button. We just

history.pushState(null, null, null);
$(window).on("popstate", function (event) {
  if (!event.originalEvent.state) {
    history.pushState(null, null, null);
    return;
  }
});

paste this code into the JS we use on all pages.

Then I will explain what I am doing.

First of all, the history object in the window object below provides methods for manipulating the browser history.

This time, this method is used to clear the previous history immediately after page transition,

and the history object has a pushState method, which can be used to delete the history.

Pass the state object as the first argument of the pushState method, the title as the second argument, and the URL as the third argument.

This time, we do not want to leave the current history at the transition timing, so pass null for all the arguments and change it to an empty history.

history.pushState(null, null, null);

In the above, only the process at the time of the first access can be supported.

When the back button is pressed, the browser tries to keep history, so you have to delete it immediately.

ADVERTISEMENT

Let’s define the popstate event using the on method to cope with this.

The popstate event is an event that fires when trying to manipulate the browser history. Fires when the browser’s Back or Forward button is pressed.

The passed event event object contains a property called originalEvent.

$(window).on("popstate", function (event) {
  if (!event.originalEvent.state) {
    history.pushState(null, null, null);
    return;
  }
});

This has also prevented the process of leaving a history when you press the back button.

Summary

What did you think?

This can easily disable browser back and prevent it from returning.

Isn’t it particularly useful for sites that frequently change data in form submissions?

Tags: back buttonJavaScript
ShareTweetShare
Previous Post

[JS] 2 == 1 = 0 is true

Next Post

Linux find -exec rm -r report No such file or directory

npn

npn

Related Posts

What is the location of the MySQL databases?
Linux

What is the location of the MySQL databases?

April 24, 2022
590
Top 10 Best WordPress SEO themes of 2022
Web development

Top 10 Best WordPress SEO themes of 2022

March 16, 2022
481
Gmail – Gmail Sign Up – Gmail Login
Web development

Gmail – Gmail Sign Up – Gmail Login

August 30, 2021
7.1k
Configuring VS Code for Node/JavaScript Development
Javascript

Configuring VS Code for Node/JavaScript Development

August 2, 2021
1.3k
How does Nodejs solve the problem of high concurrency?
Javascript

How does Nodejs solve the problem of high concurrency?

July 18, 2021
1.3k
How to create a self-signed SSL certificate for Apache on Ubuntu 16.04
Linux

How to create a self-signed SSL certificate for Apache on Ubuntu 16.04

July 18, 2021
1k
Next Post

Linux find -exec rm -r report No such file or directory

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner