Javascript Popstate event prevent Ajax request

Ching Ching

i had a problem about preventing ajax request on Popstate event.

so, this is my code :

$(window).on('popstate', function(e) {
        var state = e.originalEvent.state;
            if (state !== null) {


    loadProducts(location.pathname);
        }

});

function loadProducts(url) {

    $.ajax({
            type        : "GET", 
            url         : url, 
            dataType    : "html",
            cache       : true,
            success     : function(checks) {
                            $(".ajaxpage").html(checks);
                    }
            });
}; 

$('#product').on("click", ".paging a", function(e) {
    e.preventDefault();
    history.pushState(null, null, $(this).attr('href')); 
    loadProducts($(this).attr('href'));
});

pushstate event works really well with ajax request. but when i hit "back button" the page will do the same ajax request again. why is that? i don't need ajax anymore cause i already visit that page? just like what Github does?

how to prevent that ajax request on popstate event?

what's wrong with my code?

MegaMathias

This line is wrong

var state = e.originalEvent.state

You should access the state by writing

var state = e.state

For reference, https://developer.mozilla.org/en-US/docs/Web/Events/popstate

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ajax, prevent multiple request on click

From Dev

$ not defined ajax request in javascript

From Dev

Click event not getting triggered after Ajax request

From Dev

Ajax request on a submit button? (for analytics - Event Tracking)

From Dev

Prevent Javascript event when clicking on a button

From Dev

Javascript - AJAX request inside loops

From Dev

Get AngularJs $scope in History popstate event?

From Dev

Javascript: Prevent mouse click event moving the cursor (prevent it changing selection)

From Dev

prevent object prototype functions to be sent along with object attributes by ajax request

From Dev

Javascript ajax request

From Dev

Is there a way to delay a popstate event?

From Dev

Disable / override anchor jump from a popstate event in Safari

From Dev

Prevent multiple Ajax request (pagination with pushstate)

From Dev

How to prevent data from being lost with this ajax request

From Dev

Javascript to jquery Ajax request

From Dev

Different Ways to Bind a popstate Event

From Dev

Prevent event to be registered multiple times JavaScript

From Dev

Send ajax request on click event in backbone js

From Dev

Is it possible to prevent popstate in Javascript?

From Dev

HTML5 popstate event passed null

From Dev

Fancybox on click event with ajax fires multiple ajax request

From Dev

Javascript ajax request

From Dev

AJAX Request and on page javascript not registering?

From Dev

Prevent multiple Ajax request (pagination with pushstate)

From Dev

Prevent event to be registered multiple times JavaScript

From Dev

how to prevent a click event on a button if it is already processing a current request

From Dev

Javascript callback function in ajax request

From Dev

Is history.state always the same as popstate event.state?

From Dev

Jquery: Ajax POST request / success event not fired

Related Related

  1. 1

    Ajax, prevent multiple request on click

  2. 2

    $ not defined ajax request in javascript

  3. 3

    Click event not getting triggered after Ajax request

  4. 4

    Ajax request on a submit button? (for analytics - Event Tracking)

  5. 5

    Prevent Javascript event when clicking on a button

  6. 6

    Javascript - AJAX request inside loops

  7. 7

    Get AngularJs $scope in History popstate event?

  8. 8

    Javascript: Prevent mouse click event moving the cursor (prevent it changing selection)

  9. 9

    prevent object prototype functions to be sent along with object attributes by ajax request

  10. 10

    Javascript ajax request

  11. 11

    Is there a way to delay a popstate event?

  12. 12

    Disable / override anchor jump from a popstate event in Safari

  13. 13

    Prevent multiple Ajax request (pagination with pushstate)

  14. 14

    How to prevent data from being lost with this ajax request

  15. 15

    Javascript to jquery Ajax request

  16. 16

    Different Ways to Bind a popstate Event

  17. 17

    Prevent event to be registered multiple times JavaScript

  18. 18

    Send ajax request on click event in backbone js

  19. 19

    Is it possible to prevent popstate in Javascript?

  20. 20

    HTML5 popstate event passed null

  21. 21

    Fancybox on click event with ajax fires multiple ajax request

  22. 22

    Javascript ajax request

  23. 23

    AJAX Request and on page javascript not registering?

  24. 24

    Prevent multiple Ajax request (pagination with pushstate)

  25. 25

    Prevent event to be registered multiple times JavaScript

  26. 26

    how to prevent a click event on a button if it is already processing a current request

  27. 27

    Javascript callback function in ajax request

  28. 28

    Is history.state always the same as popstate event.state?

  29. 29

    Jquery: Ajax POST request / success event not fired

HotTag

Archive