Jump to content

Help with AJAX navigation. How do I disable normal link behavior?


ttocskcaj

Recommended Posts

I'm using jQuery and HTML5 to try and create an AJAX navigation.

I have the navigation in standard <a> tags like so:

<a id="forums" href="{url route="forums"}">Forums</a>

When JS is enabled on the browser, I don't want the browser to follow the link, but rather have AJAX take care of it (The AJAX part works fine).

 

What I'm stuck on, is disabling the default behavior of the link.

I've tried using preventDefault(), but it seems to have no effect??

Here's the code that handles the PushState/AJAX stuff.

$(document).ready(function() {
    $('a#home').click(function(){
        preventDefault()
        history.pushState({state:'new'},'Home','{$baseaddress}');          
        readurlstate();
                    
    });
    $('a#forums').click(function(){
        preventDefault()
        history.pushState({state:'new'},'Forums','{$baseaddress}forums/');
        readurlstate();
    });
    $('a#account').click(function(){
        preventDefault()
        history.pushState({state:'new'},'Account','{$baseaddress}account/');
        readurlstate();
    });
});
function readurlstate() {
    $("#content").html('<p>Loading...</p>');
    $.ajax({
        url: document.location.href
    }).done(function( html ) {
        $("#content").hide().html(html).fadeIn("fast");
    });
};

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.