3

I haven't explained this well.

But what i mean is, if I have an ajax script that loads the content of a page in a DIV element, through the function 'loadpage('whatever.php');, instead of going around manually doing this to all links, is there a way of having a script that automatically makes all regular links load through that ajax function?

Like on Facebook, your profile loads through ajax, yet if you look at their code, they just have a regular link to the profile.

Cheers!

Tom
  • 1,068
  • 2
  • 25
  • 39

4 Answers4

7

Sure, you can do it with jQuery.

This script goes through the document, finds every anchor element and binds an event handler to the click event of each. When the anchor element is clicked, the event handler finds the href attribute and loads that page into #targetDiv (you can call this div whatever you want, of course).

<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
  $("a").click(function() {
    $("#targetDiv").load(($(this).attr("href") + " body");
    return false;
  });
});
</script>

...

<!-- In your document body, this is the div you'd load the pages into. -->
<div id="targetDiv"></div>
Matt Howell
  • 15,750
  • 7
  • 49
  • 56
  • Hmm. I can't seem to get this script working, even though I've set it up all correctly? – Tom Sep 19 '09 at 18:48
  • The syntax is correct, as far as I can tell. Be sure you've included jQuery in the head of your document. Also, make sure you change #targetDiv to the name of the div you're actually using on your page. – Matt Howell Sep 19 '09 at 20:24
  • oh, and what type of URL can I put? is it only internal links? would /folder/folder.php be valid? or /folder/folder2 if folder2 went to a pHP script through .htaccess? – Tom Sep 20 '09 at 14:38
  • because when I try to 'load' a page, it updates the div, but just appears blank. – Tom Sep 20 '09 at 14:41
  • Any link on the same domain should work -- if the URL is redirected via .htaccess, it shouldn't matter. – Matt Howell Sep 20 '09 at 18:50
  • Okay, that's good. :) So why do you think the div just updates with a blank page? – Tom Sep 21 '09 at 16:39
  • Hard to tell without seeing the content of the pages that you're loading. Have you tried loading a simple page, like Test? – Matt Howell Sep 21 '09 at 17:21
  • sorry for my late reply. I just tried a simple page now, and it didn't work. – Tom Sep 24 '09 at 18:41
  • there is a little syntax error in your code snippet: after .load there are two (( instead of one ( needed! – Alphacoder Nov 06 '17 at 16:38
1

You can use JQuery for this (if I understand your question right).

First you can make the function loadpage() as follows:

function loadpage(divId, url) {
    $('#' + divId).load(url);

    return false;
}

.load() isn't supported by all browsers though. If you want to do this without .load() then you can check out .get(). For more info on .load(), take a look at http://docs.jquery.com/Ajax/load

1

I'm assuming it would go something like this:

$(document).ready(function(){
    $("a").click(function(){
        $("body").load($(this).attr("href") + " body");
        return false;
    });
});

This would make all <a> tags on the page call a function that downloads a HTML document from the href attribute of the tag, strip out it's body tag, and replace the contents of the current page's body tag with the contents of the new body tag. This way, it's easier to work this with no JavaScript, as well as integrate it into an existing site.

To use it, you place this into a <script> tag in the head of your main page, or in an external JS file.

Please note, however, that this code only updates the contents of the <body> tag, the head (including the title tag) remains untouched. You may need to add extra code to update things like this.

MiffTheFox
  • 21,302
  • 14
  • 69
  • 94
  • Cheers for the fast reply (I would have replied to the others, but this seemed like the best solution). I'm not too familiar with jquery but it looks good so I've set it up. Where would I place this to use this and what effect would it have? Sorry to be so ignorant! – Tom Sep 19 '09 at 18:02
  • what if the reply was in json and you needed to take the json and based on that re-render a div? – Val Neekman Aug 04 '10 at 23:49
  • @VN44CA - If the reply would be JSON, it wouldn't exactly be a "normal link" link in the OP, would it? – MiffTheFox Aug 05 '10 at 01:09
1

Simple and Nice. Check this out: Bjax

Usage:

<script src="bjax.min.js" type="text/javascript"></script>
<link href="bjax.min.css" rel="stylesheet" type="text/css" />

Finally, include this in the HEAD of your html:

$('a').bjax();

For more settings, checkout demo here: Bjax Demo

endur
  • 682
  • 11
  • 13