3

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

1 Answer 1

7

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);
});
1
  • 1
    Wow, that simple? Thanks alot
    – TaylorMac
    Commented Jan 14, 2012 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.