Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the title of a requested page using jQuery AJAX

Tags:

jquery

ajax

I have written the following to do AJAX requests in my app:

$(document).ready(function () {

    $('ul#ui-ajax-tabs li:first').addClass('selected');

    $('#ui-ajax-tabs li a').click(function (e) {

        e.preventDefault();

        $("#ui-ajax-tabs li").removeClass("selected");

        $(this).parents('li').addClass("loading");

        var url = $(this).attr("href");
        var link = $(this);
        console.log(url);

        $.ajax({
            url: url,
            success: function (responseHtml) {
                $('div#ui-tab-content').html($(responseHtml).find('div#ui-tab-content > div#ui-ajax-html'));
                $(link).parents('li').addClass('selected');
                $("#ui-ajax-tabs li").removeClass("loading");
            },
            error: function () {
                $('div#ui-tab-content').html('<div class="message error">Sorry that page doesn\'t exist</div>');
                $(link).parents('li').addClass('selected');
                $("#ui-ajax-tabs li").removeClass("loading");
            }
        });

    });

});

How would I grab the title from the requested page and use it? I've thought about something like: var title = $(document).attr('title'); but how do I get the title from the requested page and set that as the new title? Thanks

like image 829
Cameron Avatar asked Sep 29 '11 15:09

Cameron


1 Answers

I am new to jQuery, but I will give it a try anyways:

var newTitle = $(responseHtml).filter('title').text();

And if one of the above works, the current title could be changed by

document.title = newTitle;

Again I am a jQuery and Javascript beginner and just wanted to give it a try :) So excuse me if its stupid and wont work :)

like image 186
r0skar Avatar answered Nov 10 '22 01:11

r0skar