KeithRules
KeithRules

Reputation: 207

Get a title of a page loaded with .load

I load content of the page at a clicked link's href using .load(), like the following:

$("html").on('click', 'a', function(event) {
    event.preventDefault(); // obviously
    var url = this.href + " #main"; // holds the content of #main at a given url
    $('#ajax-container').load(url); // loads that content into #ajax-container
    document.title = "?";
});

How can I grab the title of the loaded page and display it?

Upvotes: 0

Views: 98

Answers (1)

A. Wolff
A. Wolff

Reputation: 74420

Try this maybe:

var url = this.href; //following techfoobar's comment

$('#ajax-container').load(url,function(data){
    document.title = $($.trim(data)).find('title').text();//$.trim() used for old IE to move unexpected characters
}); 

But then, you won't gat what you expect as content in $('#ajax-container')

So use $.get() instead:

var url = this.href; //following techfoobar's comment

$.get(url,function(data){
     document.title = $('<div/>').html($.trim(data)).find('title').text();
     $('#ajax-container').html($('<div/>').html($.trim(data)).find('body').html());
});

Upvotes: 3

Related Questions