Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2005, 07:15 PM
laredotornado@zipmail.com
Guest
 
Posts: n/a
Default how to get page title?

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
Default 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
Default 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
Default 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
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.