Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect browser incompatibility for pushState function

This function disables the default action for a link, and changes the URL using the pushState function. I need to be able to detect if a browser does not support this function, so that I can stop the preventDefault() function.

$("a").click(function(event) {      

            var url = "";
            var url = $(this).attr('href'); 

        // Disable Default Action and Change the URL -  
        event.preventDefault();     
        window.history.pushState("somedata", "Title", url);

        //Call Function to change the content - 
        loadContent(url);
    });

Any recommendations are greatly appreciated

like image 209
TaylorMac Avatar asked Dec 01 '25 13:12

TaylorMac


1 Answers

Use feature detection:

if (history.pushState) {
  // supported.
}

Example:

$("a").click(function(event) {      
    var url = "";
    var url = $(this).attr('href'); 

    if (history.pushState) {
        window.history.pushState("somedata", "Title", url);
        event.preventDefault();
    }

    //Call Function to change the content - 
    loadContent(url);
});
like image 149
Arnold Zokas Avatar answered Dec 04 '25 02:12

Arnold Zokas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!