1

Is it possible to load all the links with class 'ajax' without refreshing the page? The complete body content should be changed, the page title and the url should so it can be bookmarked.

3
  • 1
    Have a look at pjax pjax.heroku.com Commented Jul 28, 2011 at 10:42
  • @Dogbert that script is massive for what effectively should a few lines of jQuery. edit- The example on the sites homepage doesn't even work. Commented Jul 28, 2011 at 10:51
  • 1
    @Dunhamzzz, it does but you have to tick the checkbox. Agreed though, too much. Commented Jul 28, 2011 at 11:04

2 Answers 2

3
$('a.blah').click(function(e){
    e.preventDefault;
    $.get('page.html', function(data){
        $('body').html(data);
    });
});

That is probably the simplest way, replace blah with your class, and page.html with your page.

Sign up to request clarification or add additional context in comments.

Comments

2

This should work for all links with a certain class, 'target' is the id of your main content div. It requires no editing of your current mark up.

$('.class').click(function(e) {
    e.preventDefault();
    $('#target').load($(this).attr('href'));
});

5 Comments

For the difference between my response and Dunhamzzz, read this: stackoverflow.com/questions/1246137/…
@rickyduck .load is fine for OPs needs?
thought it relevent to highlight the difference for future ref, knowledge is key! $.load is fine though, I'd use $.load in this instance
But can i change the page title and url with this?
Yeah, depends how you want to do it, it will require some jQuery knowledge. Personally, I'd put the page title in the <a title="" attribute, or you could be more advanced and fetch it from the ajax reponse.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.