views:

160

answers:

1

Hi, I'm not very elite when it comes to JavaScript, especially the syntax. So I'm trying to learn. And in this process I'm trying to implement a content loader that basically removes all content from a div and inserts content from another div from a different document.

I've tried to do this on this site: www.matkalenderen.no - Check the butt ugly link there. See what happens?

I've taken the example from this site: http://nettuts.s3.cdn.plus.org/011_jQuerySite/sample/index.html#index

But I'm not sure this example actually works the way I think it does. I mean, if the code just wipes out existing content from a div and inserts content from another div, why does the other webpages in this example include doctype and heading etc etc? Wouldn't you just need the div and it's content? Without all the other stuff "around"? Maybe I don't get how this works though. Thought it worked mosly like include really.

This is my code however:

$(document).ready(function() {

    var hash = window.location.hash.substr(1);
    var href = $('#dynloader a').each(function(){
            var href = $(this).attr('href');
            if(hash==href.substr(0,href.length-5)){
                    var toLoad = hash+'.html #container';
                    $('#container').load(toLoad)
            }                                                                                        
    });

    $('#dynloader a').click(function(){

            var toLoad = $(this).attr('href')+' #container';
            $('#container').hide('fast',loadcontainer);
            $('#load').remove();
            $('#wrapper').append('<span id="load">LOADING...</span>');
            $('#load').fadeIn('normal');
            window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
            function loadcontainer() {
                    $('#container').load(toLoad,'',showNewcontainer())
            }
            function showNewcontainer() {
                    $('#container').show('normal',hideLoader());
            }
            function hideLoader() {
                    $('#load').fadeOut('normal');
            }
            return false;

    });

});
+2  A: 

The jQuery .load() has some extra functionality in it to support this.
You can see it in the docs here (look at `Loading Page Fragments')

In your example here's the important part: hash+'.html #container' the space before the #container means that jQuery will grab that URL, look for the element with id="container" and fetch only that element's content's discarding the rest of the page. This lets .load() work in a more generic way without what you're fetching being specifically designed for only ajax loading.

In your case I think you just need to remove #content from the end unless you're looking for that element. It's not saying "look for content" that's just the ID they used for the element they wanted to look for. It could have been #divToLoad which would be a clearer example IMO.

Nick Craver
Did you look at the original source or the version I've slightly altered? Not sure I understand the last part "In your case I think you just need to remove #content from the end", since I ain't got no #content in my version.
Kenny Bones
@Kenny - In the code you posted this is on the 6th line: `var toLoad = hash+'.html #container';` If you're not looking inside the content for that ID, it should be just: `var toLoad = hash+'.html'` the same in your click handler: `var toLoad = $(this).attr('href')+' #container';` should be `var toLoad = $(this).attr('href');`
Nick Craver
Ok I see. But to take it a bit further. The code as it is gets content from a div within a file. That's good. But that would require alot of single files with content. Even if there's just a little content in each file. Would it be possible to put all of the content into a single file? Only putting them in separate divs? Preferrably without having to specify the IDs in the js code? Not sure how that could be done though, perhaps via the href?
Kenny Bones
@Kenny - Yes you can do this via a different method (`$.get()` or `$.ajax()`), but you'll need the IDs or classes or something unique so you can find the elements you want.
Nick Craver
Why would I need a different method? Just read about the get method and it looks the same as load doesn't it?
Kenny Bones
@Kenny - The `$.get` method has a success callback, easier to use it and do your content setting in the success function rather than piggyback off one element's load, since no element is getting all the content anyway, much cleaner/easier to read as well.
Nick Craver
Ok, so I could combine the code I've already got and just use $.get() instead? Would have to read up on it a bit I guess. Not sure how this works and would work in my scenario. But I can hardly think that what I'm after is unique in any way? What's the normal way of doing stuff like this? Cause all I really want to do is essentally load some stuff that might be php content into a div. More like what Facebook is doing, rather than having to load an entire new page like normal href.
Kenny Bones
@Kenny - Think about what you want the result to be and post a new question with that...comment about it here, I'll make sure to post a full answer that fits your case there if you haven't gotten one by the time I see it. Don't want to clutter up this question as it is different (makes it less useful for those that find it later...and comments aren't the best for showing code)
Nick Craver
Good thinking Nick :) I'll do just that :) Thanx!
Kenny Bones