views:

176

answers:

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!

+4  A: 

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"&gt;
</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>
bigmattyh
Hmm. I can't seem to get this script working, even though I've set it up all correctly?
Tom
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.
bigmattyh
oh, and what type of URL can I put? is it only internal links? would/folder/folder.php be valid?or /folder/folder2if folder2 went to a pHP script through .htaccess?
Tom
because when I try to 'load' a page, it updates the div, but just appears blank.
Tom
Any link on the same domain should work -- if the URL is redirected via .htaccess, it shouldn't matter.
bigmattyh
Okay, that's good. :) So why do you think the div just updates with a blank page?
Tom
Hard to tell without seeing the content of the pages that you're loading. Have you tried loading a simple page, like <html><head></head><body>Test</body></html>?
bigmattyh
sorry for my late reply. I just tried a simple page now, and it didn't work.
Tom
+1  A: 

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

Kristinn Örn Sigurðsson
+1  A: 

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
good suggestion to target the body of the link's URL.
bigmattyh
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
@Tom - Updated answer
MiffTheFox
this works, but it just loads a blank page?
Tom
what if the reply was in json and you needed to take the json and based on that re-render a div?
VN44CA
@VN44CA - If the reply would be JSON, it wouldn't exactly be a "normal link" link in the OP, would it?
MiffTheFox