Ajax, page titles, etcetera

In wanting to do partial page refreshes (load new content into a specific div) by Ajax, I’ve been trying (unsuccessfully) to find some code which’ll maintain history/back button and display correct page title.

The correct page title seems to be a problem, not just for me but many others who’ve posted in various forums.

Having searched a lot, I came across JQueryMobile, from which I quote (http://demos.jquerymobile.com/1.2.1/docs/pages/page-titles.html):


When you load the first page of a jQuery Mobile based site, then click a link, Ajax is used to pull in the content of the requested page. Having both pages in the DOM is essential to enable the animated page transitions, but one downside of this approach is that the page title is always that of the first page, not the subsequent page you’re viewing.
To remedy this, jQuery Mobile automatically parses the title of the page pulled via Ajax and changes the title attribute of the parent document to match.

Seems ideal, but as a marketer rather than a tech-minded person, I’ve thus-far been unable to understand how to make it work.

And, if there’s an alternative solution, I’ll welcome that before what’s left of my diminishing brain totally melts.

Technically, it’s very easy to change page title with jQuery:

$('title').text('A New Title');

The tricky part is how to know what title should new “page” have.
Possible solution is to get a title from H1 tag of each AJAX page.
For example, HTML code:

<!-- page.html -->
<h1>My New Page</h1>
<p>Content of the page</p>

jQuery code:

//load page with AJAX
$.get('page.html', {}, function(html){
    /* parse new title from H1 tag */
    var newTitle = $(html).find('h1').text();
    /* change current title to the new one */
    $('title').text(newTitle);
    /* put received HTML into corresponding div */
    $('#content').html(html);
});

Thanks.
I’m sorry for not replying - not getting notifications of replies.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.