The functionality of the back button in a JavaScript website only allows users to navigate through their browsing

I'm currently experiencing an issue with the back navigation button on my one-page website. When I load a new page, I use the following code snippet:

displayPage = function (page, json) {
        var stateObj = { "page": page, 'json': json };
        history.pushState(stateObj, "", page);
        // some code here

and then when the back button is triggered:

$(function() {
    Skeleton._construct();
    window.onpopstate = function(event) {
        Skeleton.dispPage(event.state);
    }
});

this.dispPage = function(blob){
        displayPage(blob.page, blob.json);
}

However, after clicking the back button once, the onpopstate event is triggered and I successfully go back to the previous page. But if I click the back button again, or multiple times, the onpopstate event is no longer triggered. Can anyone advise me on what I should do in this situation?

Thank you in advance for your help.

Answer №1

After investigating the issue independently, I identified the root cause.

Upon calling the displayPage function, I neglected to check the source of the reloads, resulting in a repeated execution of pushState and interfering with the navigation history. To resolve this, I made the following adjustments:

this.dispPage = function(blob){
        //added a parameter here
        displayPage(blob.page, blob.json, 1);
}

displayPage = function (page, json, external = 0) {
        var stateObj = { "page": page, 'json': json };
        // checked the 'external' parameter here
        if(external == 0){
          history.pushState(stateObj, "", page);
        }
        // implemented some code here
}

Sharing this solution in hopes that it may assist others facing similar challenges.

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

Send the object containing a Dictionary<string, int> parameter to the WebMethod

I encountered an error that says: "Cannot convert object of type 'System.String' to type 'System.Collections.Generic.Dictionary2[System.String,System.Int32]'","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObje ...

The Handlebars helper needs to provide a result when a value is present

I have created a Handlebars helper function like this: Here is the code in my JavaScript file: Handlebars.registerHelper('switch', function (sw_val, options) { if (sw_val != '*NONE' && sw_val != false && sw_val != ...

There seems to be an issue with npm displaying an inaccurate version number for my

In brief: The version of my module on npmjs.org doesn't match the version in package.json. Why? I have a JavaScript module that I released on npm and bower: https://github.com/Offirmo/network-constants.js It has a package.json for npm and a bower.js ...

Shuffle math calculations involving subtraction by a percentage using node.js or JavaScript

Hello there! If you want to subtract, say 35%, from a number, you can use methods like this: var valueInString = "2383"; var num = parseFloat(valueInString); var val = num - (num * .35); console.log(val); But have you ever wondered how you could randomiz ...

Is it possible to alter the state of one page by clicking a link on another page?

Is it possible to update the state of a different page when a link is clicked and the user navigates to that page? For example: Homepage <Link href="/about"> <button>Click here for the About page</button> </Link> If a ...

Click once to change the element, but it will remain changed and will not revert back no matter how many times you

After the first click displaying "X" and then changing to "O" in my tic-tac-toe JavaScript game, subsequent clicks only show "O", failing to switch back to "X". I am currently working on creating a tic-tac-toe game using JavaScript. Upon the first click, ...

Issues with displaying AngularJs directive template

Having an issue with my AngularJs directive. Everything works perfectly fine when I use the "template" attribute, but when I switch to using "templateURL", it stops working. Both the JavaScript file for the directive and the HTML file for the template are ...

Tips for employing the slice approach in a data-table utilizing Vue

I have a unique situation in my Vue app where I'm utilizing v-data table. The current display of data in the table works fine, but I now want to enhance it by incorporating the slice method. Here is the current data displayed in the table: And here ...

How do you send a variable in a GET request with React?

My challenge is to retrieve data from a table where the teacherId matches the teacherId of the user who logs in, but I am facing difficulties in passing this teacherId from the front-end to the back-end. Below is the backend code: app.get("/api/get&q ...

Angular version 8.2 combined with Firebase can result in errors such as those found in the `./src/app/app.module.ngfactory.js` file towards the end of the process when attempting to build with

My first time posing a query on this platform, and certainly not my last. The issue at hand involves the ng-build --prod process failing to complete and throwing errors in my Angular 8.2.14 application. I've integrated Firebase into my project succes ...

How can transitions be activated in Bootstrap 4 carousel?

Having an issue with my Bootstrap 4 carousel where I need to move 4 slides at a time. I have tried using the code below: $(this).carousel(4); However, the problem is that the carousel only jumps to that index without showing any transitions. Using $(this) ...

The custom service is failing to load, in the simplest terms possible

After creating a dataService.j that contains the following: angular.module('dataService', []) .service('gameDataService', function() { var _gameData = { loggedIn: "false", gameJoined:"false", tableFu ...

The AngularJs project is currently experiencing issues when trying to function as a single page web application

I'm currently working on incorporating AngularJS into an existing web project that uses jQuery. Here is a snippet of my code: <div class="medya" ng-repeat="d in data" masonry-item-dir> <a href="{{d.Url}}" class="onizleme"><img src="{ ...

Utilizing i18next-express-middleware for seamless language switching based on user input

As a newcomer to node.js, I am looking to internationalize my application. To kickstart this process, I have utilized the template available on i18next-express-middleware github in conjunction with resources from the i18next website. My goal is to enable u ...

Eliminate the ending slash from your Nuxt application

Hello, I am currently working on a Nuxt Application and encountering an issue. After running the npm run build command and starting it with npm start, there seems to be an inconsistency with trailing slashes. During the build, the URL appears without a t ...

Parsing JSON data using Swift

I need assistance with making an API call to the GitHub API in order to retrieve the names of folders within a repository. I'm unsure about how to extract and process the data returned from the API call. Any guidance or help on this matter would be gr ...

Utilizing Jquery and JavaScript to filter out specific HTML objects retrieved from an AJAX response

I'm encountering a puzzling issue with this snippet of HTML: <div id="1"> <div class="text"> Text for div 2 </div> <img src="images/image1.jpg"></img> </div> <div id="2"> <div class="text"> ...

Different results can be observed when comparing the array ordering in JavaScript between IE8 and Chrome

The array presented with items listed in specific order: { "5":{ "Title":"Title A", "Desc":"Description A" }, "15":{ "Title":"Title B", "Desc":"Description B" }, "10":{ "Title":"Title C", "Desc":"Description C ...

CombineReducers takes all the reducer content and unpacks it into a single reducer

As I work on developing a small application, I am contemplating the best strategy for organizing reducers within it. My objective is to retrieve JSON data from the application state and structure it as shown below: { "fruits": [ {"appl ...

What causes the slash URL to behave differently than other URLs when running through the middleware of NodeJS?

When I type http://localhost:3000/product into the browser, why do I see output for both '/' and '/product'? Take a look at this code snippet below. const express = require('express'); const app = express(); // http://loca ...