Connecting Tech Pros Worldwide Help | Site Map

how to get page title?

  #1  
Old September 5th, 2005, 07:15 PM
laredotornado@zipmail.com
Guest
 
Posts: n/a
Hello,
How do I use Javasript to get the <TITLE> element of a web page?
If anyone can supply a cross browser way (IE 5.5+, Firefox 1+), it is
most appreciated.

Dave

  #2  
Old September 5th, 2005, 07:25 PM
Martin Honnen
Guest
 
Posts: n/a

re: how to get page title?




laredotornado@zipmail.com wrote:
[color=blue]
> How do I use Javasript to get the <TITLE> element of a web page?[/color]

document.title gives you the text content of the <title> element. You
can also set document.title in most current browsers.

The element itself can be accessed like any other element using e.g.
var titleElement;
if (document.getElementsByTagName) {
titleElement = document.getElementsByTagName('title')[0];
if (titleElement) {
// use titleElement here e.g.
alert(titleElement.nodeName);
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #3  
Old September 6th, 2005, 02:35 AM
Danny
Guest
 
Posts: n/a

re: how to get page title?



Either as Martin indicated using DOM, or using:
document.title='YOUR TITLE HERE';


Danny
  #4  
Old September 6th, 2005, 04:40 AM
Mick White
Guest
 
Posts: n/a

re: how to get page title?


Danny wrote:
[color=blue]
> Either as Martin indicated using DOM, or using:
> document.title='YOUR TITLE HERE';
>
>
> Danny[/color]

Or:
top.document.title='YOUR TITLE HERE';

Mick
  #5  
Old September 6th, 2005, 06:15 AM
RobG
Guest
 
Posts: n/a

re: how to get page title?


Danny wrote:[color=blue]
> Either as Martin indicated using DOM, or using:
> document.title='YOUR TITLE HERE';[/color]

document.title is included in the DOM HTMLDocument interface, so to that
extent it is 'DOM':

<URL:http://www.w3.org/TR/2000/WD-DOM-Level-2-HTML-20001113/html.html#ID-26809268>

It is both a 'getter' and a 'setter' and is generally displayed in a
browser's window title bar.


Using Martin's getElementsByTagName method will return a reference to
the title element, but the actual value is the content, not the element
itself. IE refuses to believe that there are any nodes inside the title
- but it will return a value for innerHTML.

So to get the actual text content of the title element you need to
access the firstChild, or for IE use innerHTML:

function showTitle()
{
var t = document.getElementsByTagName('title')[0];
if ( !!t.childNodes.length ) {
alert( t.firstChild.data );
} else if ( t.innerHTML ) {
alert( t.innerHTML );
}
}

You can set the value of the element much the same way, but IE will not
let you modify the innerHTML:

function changeTitle( txt ){
var t = document.getElementsByTagName('title')[0];
if ( !!t.childNodes.length ) {
t.firstChild.data = txt;
} else if ( t.innerHTML ) {
alert( 'Out of luck' );
// t.innerHTML = txt; // uncomment to see error in IE
}

Changing the title element this way does not seem to modify the title
shown in the window title bar (Firefox & IE).

Given all the above, using document.title as a getter and setter gets a
big thumbs up:

function showTitle(){
alert( document.title );
}

function changeTitle( txt ){
document.title = txt;
alert( 'Title changed to ' + document.title );
}



--
Rob
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
URL and Page title in PHP deepaks85 answers 3 February 20th, 2009 08:23 AM
Set Page Title in code behind Nevyn Twyll answers 6 November 19th, 2005 03:46 PM
Page title in ASP.NET pages Maxim Izbrodin answers 5 November 18th, 2005 04:19 AM
How to dynamically change page title using php and require_once? the wonderer answers 6 July 17th, 2005 01:39 AM