views:

528

answers:

1

So far...

$('#container').load(hash + ' #page','', function() { 
    $('#container').fadeIn('fast');
    document.title = $('#title').load(hash + ' #title').text();
    });

...doesn't work. Is there a better/correct way to do this?

FYI: -

  • I've added the ID #title the tag (all pages/it's a PHP template).
  • The container is .fade(ed)Out beforehand (less important)

Thanks in advance.

+2  A: 

The problem is that, at the time you assign to document.title, the $('#title').load(hash + ' #title').text() might not have finished yet. Try setting the new document.title in the callback of that .load.

UPDATE

Try:

$('#container').load(hash + ' #page','', function() { 
    $('#container').fadeIn('fast');
    $('#title').load(hash + ' #title', '', function(data) {
        document.title = $(this).text();
        });
    });
Nikki Erwin Ramirez
Thanks Nikki, this works great! Thanks for the explanation; the title was initially appearing briefly then disappearing.I'm wondering if there's a way to make "one call/load", fetching the #page contents and #title text?I (wishfully) tried the following (not so good): - $('#ajax, #title').load(hash + ' #page, #title','', function() { document.title = $('#title').text();$('#ajax').fadeIn('fast'); $('#loading').remove();});An extra call won't hurt ;-). Thanks again.
LeslieOA
If you really want to reduce it to a single call (in case you have more portions to retrieve, not just `#title` and `#page`), maybe you can pre-load the page in a hidden `div`, then read from there.
Nikki Erwin Ramirez
I settled on adding a 'title' attribute to page main header tags (e.g. <h2 title="News Articles">News</h2>) and reading it from there. Mildly hacky, but decent. Thanks again!
LeslieOA
To anyone interested, I'm using a HTML5 data attribute 'data-title' to the each #page. More here: - http://ejohn.org/blog/html-5-data-attributes/
LeslieOA