JQuery.load (or $ .ajax) to get and set the page title?

Still...

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

... does not work. Is there a better / right way to do this?

FYI: -

  • I added the ID #title tag (all pages / this is a PHP template).
  • The container has .fade (ed) Out in advance (less important)

Thanks in advance.

+7
javascript jquery ajax
source share
5 answers

The problem is that when assigning document.title parameter $('#title').load(hash + ' #title').text() may not end. Try setting a new document.title in the .load .

UPDATE

Try:

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

I had the same requirement to update the page title after ajax loading and what I used was a single request, no special attributes needed to be set in your HTML:

 var contentWrap = $('#content-wrap'), contentId = '#content'; $.ajax({ url: hash.replace(/^#!\//, '') + '.html', success: function(text) { var oldContent = contentWrap.find(contentId), newPage = $(text), newContent = newPage.find(contentId); oldContent.remove(); contentWrap.append(newContent); // Set New Title document.title = newPage.filter('title').text(); } }); 
+4
source share

Do it:

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

:)

0
source share

Try entering the code:

 .**filter**('title').text() $('#container').load(hash + ' #page','', function(result) { $('#container').fadeIn('fast'); document.title = $(result).filter('title').text(); }); 
0
source share

We wanted to have the effect that when someone visits our site, you should see a separate page name, but when you leave, you will see "We will still miss you ... :)" using the code below

 <!-- Active Navigation Script --> <script> var url = window.location; $('ul.nav a[href="'+ url +'"]').parent().addClass('active'); $('ul.nav a').filter(function() { return this.href == url; }).parent().addClass('active'); </script> <!-- Tab Title change Script --> <script> $(function() { // Get page title var pageTitle = $("title").text(); // Change page title on blur $(window).blur(function() { $("title").text("We'll still miss you... :)"); }); // Change page title back on focus $(window).focus(function() { $("title").text(pageTitle); }); }); </script> 

You can see it in action here: http://www.solutelabs.com

0
source share

All Articles