The event listener for 'popstate' is not functioning as expected in Internet Explorer 11

Is there a way to prevent the browser back button from functioning?

I attempted to restrict it using the following code:

history.pushState(null, document.title, location.href);
global.window.addEventListener('popstate', function () {
    history.pushState(null, document.title, location.href);
});

While this code successfully restricted the back button in Chrome, I encountered an issue with IE11 where it did not work as intended.

Any help or suggestions would be greatly appreciated.

Answer №1

See the example below to see it working in both Chrome and IE.

Code:

<html>
<head>
    <title>Prevent Back Button Functionality in Browser - Live Demo</title;
    <style type="text/css">
       div{
  margin:auto;
  width:400px;
  border:1px solid black;
}
h1{
  text-align:center;
}
a{
  text-decoration:none;
}
p{
  text-align:center;
}
    </style>
    <script type="text/javascript">
       history.pushState(null, null, location.href);
    window.onpopstate = function () {
        history.go(1);
    };
    </script>
</head>
<body>
   <div>
<h1><a href="http://www.justwebcode.com" rel="index,follow">Just Web Code</a></h1>
  <p>Click on Browser Back Button to Test</p>
</div>
</body>
</html>

Reference:

How to restrict browser back button functionality using JavaScript

Note:- Make sure to run the code locally as running it from the Stack Overflow snippet may cause errors.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Increasing Taxes and Boosting the Overall Cost

How can we set up a system where taxes are bypassed by default unless otherwise specified when placing an order? Let's take a look at the following text box: <input class="txt1" type="text" name="subtotal" value="" id="subtotal" size="16" ta ...

Enhancing collapsible list headers in jquery mobile with checkboxes

Having trouble with a jQuery Mobile website issue. Currently working on a jQuery mobile site that includes a collapsible list (). The client request is to have a checkbox inside the header, allowing users to check items off without needing to open them. T ...

Tips for retrieving the server's "response" with JQuery?

Take a look at the code snippet below. <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, click on Tools | Templates, then open the template in the editor. --> <ht ...

The leaflet_Ajax plugin is constantly searching for the default marker symbol located at js/images/marker-icon.png

Although I'm relatively new to Leaflet, I have experience creating interactive maps in the past. Recently, I've been working on a project involving displaying GPS data from a UAV. The UAV transmits location information to a server, which then ret ...

I am interested in extracting information from a public Facebook post

Is it possible to scrape or utilize the FB API to retrieve data from a public profile's wall post? By inspecting the element on the URL, you can see most of the data as well as the ajax calls for infinite scrolling on the wall. How could one go about ...

I am currently facing a challenge in animating my ng-show/ng-hide animation

This particular issue has been widely recognized and discussed multiple times in various forums. Despite the extensive search, I have not come across satisfactory solutions. var aniApp = angular.module("aniApp", []); aniApp.controller('mainCtrl&ap ...

Variety of positions for jquery ui tooltips

When incorporating Jquery UI tooltips into my project, I encountered a challenge with positioning the tooltips throughout the entire document except for specific divisions with a certain class. $(document).not(".t_l_overview").tooltip({ position: { ...

Is there a way to trigger a CSS3 animation to play when scrolling down and then play in reverse when scrolling up?

Using a blogger website, I have successfully set up sticky navigation. However, when scrolling back up, the navigation bar pops back into place without any animation. I am looking for a solution to smoothly return the navigation bar to its original positio ...

Change the Registry Key to "Require user input for login with username and password"

I have encountered an issue with the login prompt while using a web application that utilizes windows authentication. I am utilizing Selenium Web-driver with C# for my test automation. Scenario: My task is to test a web application that relies on windows ...

Angular 2: Utilize the select event to fetch and return the selected item

How can I pass an item on change event? Currently, my code looks like this: <select #sel (change)="select.emit(sel.value.url)"> <option *ngFor="let item of selectlist"> {{item.description}} </option> &l ...

Data in the array is only updated upon refreshing the webpage

Why is the array empty when I navigate to a new route (/category/name-of-category) that renders my Category component, but it gets data when I refresh the page? What am I missing here? To better explain, I have created a video. Video showcasing the issue: ...

Multiple dynamically created textboxes are not responding to the onblur event function causing issues

I am facing an issue with my code where I have three textboxes for retail price, quantity, and total amount. The problem arises when I enter the retail price and quantity; the total amount textbox doesn't automatically calculate the multiplication res ...

You cannot use jQuery to initiate a button click event through another button click event

I'm currently facing an issue where I want to replace one button with another, but it's not going as smoothly as I hoped. When I click the first button, everything works fine. However, when I click the second button, which is supposed to trigger ...

Navigating through reliable dropdown menus in Django

Struggling to implement a reliable dropdown feature in Django due to lack of expertise in JavaScript/ajax, I have reached a dead end. Note: Despite reviewing similar inquiries, none provided a complete solution for my issue. The Challenge: Given the size ...

Issues with npm @material-ui/core Button and TextField functionality causing errors and failures

I'm currently working on a React project and decided to incorporate the material-ui framework. After creating a component that includes a textfield and a button, I've encountered an issue where I can't interact with either of them as they s ...

Experiencing issues with a blank or non-functional DataGrid in Material UI components

My DataGrid table is showing blank. I experienced the same problem in a previous project and recreated it in a new one with updated versions of django and mui libraries. Here is an example of my data displayed with DataGrid not working I posted a bug rep ...

Developing a personalized Avada form auto-scrolling algorithm

Our form, created using the Wordpress - Avada theme, needs an autoscroll feature. As users answer questions, the next question appears below, but this is not immediately visible on mobile devices. To address this, we require autoscroll functionality. The ...

How to perform complex relational queries in TypeORM using relationship properties?

Currently, I am immersing myself in Nest.js using TypeORM and exploring its query builder capabilities. I recently came across the relations property, which proved to be quite useful. However, I'm wondering if it's possible to achieve the same o ...

The functionality of React setState seems to be malfunctioning when activated

Having encountered an unusual issue with react's setState() method. Currently, I am utilizing Azure DevOps extensions components and have a panel with an onClick event that is intended to change the state of IsUserAddedOrUpdated and trigger the addOr ...

styled-components and TypeScript - Unsafe assignment of a value with an error type

Currently, I am developing an application using Next.js and Typescript. As part of the project setup, I have integrated styled-components into the application. In order to style my components effectively, I created a new file named Card.styled.ts: import ...