Search code examples
javascriptjqueryajaxpushstatejquery-bbq

How can I avoid double submissions with pushState and $.ajax(hash) in non-IE browsers?


Internet Explorer appears to be the only major browser that does not support pushState to handle history for AJAX submissions. So everywhere I have:

$.bbq.pushState(hash);

in my AJAX code, I have to add another line:

$.ajax(hash);

This of course causes a double-submission in all non-IE browsers that DO support submission via pushState().

What can I do to avoid these double submissions?


Solution

  • Just call $.ajax(hash); in a conditional statement only if the history.pushState is not natively supported

    if (!history.pushState) {
       $.ajax(hash);
    }
    

    I also suggest to refactor your code using a unique wrapper function accepting an hash as parameter with the necessary logic, e.g.

    var pushStateWrapper = function(hash) {
    
        if (!history.pushState) {
           $.ajax(hash);
        }
        else {
           $.bbq.pushState(hash);
        }
    }