How do I change the document title?

R

Richard Trahan

(This is a repost -- the original got tucked away into an old thread
because I used the same Subject name.)

I'm trying to change the document title to add an asterisk when the
document becomes "dirty", as seen on editor applications.

I use the code below. Venkman shows that everything looks as expected.
The last line is for the debugger, which indicates that the title is
changed, but it does not display.

var newtitlenode = document.createElement("title");
var newtext = document.createTextNode("MyTitle"+(dirty?"*":""));
newtitlenode.appendChild(newtext);
var headnode = document.getElementsByTagName("head").item(0);
var oldtitlenode = document.getElementsByTagName("title").item(0);
headnode.replaceChild(newtitlenode,oldtitlenode);
headnode = document.getElementsByTagName("head").item(0);

How can I get the changed title to display?

I'm running NN 7.1.
 
M

McKirahan

Richard Trahan said:
(This is a repost -- the original got tucked away into an old thread
because I used the same Subject name.)

I'm trying to change the document title to add an asterisk when the
document becomes "dirty", as seen on editor applications.

I use the code below. Venkman shows that everything looks as expected.
The last line is for the debugger, which indicates that the title is
changed, but it does not display.

var newtitlenode = document.createElement("title");
var newtext = document.createTextNode("MyTitle"+(dirty?"*":""));
newtitlenode.appendChild(newtext);
var headnode = document.getElementsByTagName("head").item(0);
var oldtitlenode = document.getElementsByTagName("title").item(0);
headnode.replaceChild(newtitlenode,oldtitlenode);
headnode = document.getElementsByTagName("head").item(0);

How can I get the changed title to display?

I'm running NN 7.1.

<html>
<head>
<title>title.htm</title>
<script type="text/javascript">
function titled() {
document.title += '*';
}
</script>
</head>
<body>
<a href="javascript:titled()">*</a>
</body>
</html>
 
R

Richard Trahan

McKirahan wrote:

(snip)

Yes, of course! That works, thank you.

In the interest of higher education, what was wrong with my node method?
 
R

RobG

Richard said:
McKirahan wrote:
Yes, of course! That works, thank you.
In the interest of higher education, what was wrong with my node method?

It would seem your code works - insert the following
immediately after your replace call:

var x = document.getElementsByTagName("title").item(0);
alert('The ' + x.nodeName
+ ' now has value: '
+ x.firstChild.nodeValue
+ '\nand document.title is: '
+ document.title);

In Firefox and IE the TITLE has been replaced. FF
reports document.title as the old one and IE makes
it empty (but doesn't change the title in the window
title bar).

I can only guess that "title" is created when loading
the page and isn't refreshed it if part of the <head>
changes. Maybe you can play with document.write or innerHTML.
 
Y

Yann-Erwan Perio

Richard Trahan wrote:

[changing title of the document]
In the interest of higher education, what was wrong with my node method?

According to the DOM/HTML specification the HTMLTitleElement inherits
from HTMLElement, which itself inherits from the core Element interface,
which gives it appropriate DOM methods. Since HTML defines the content
of TITLE as PCDATA, the title should be able to have text nodes and
change their node values.

However, AFAICS, the specification does not state that the [normalized]
text nodes inside the title should be considered as the title's value:)

While IE has therefore decided to not create a text node, Mozilla and
Opera have however made the decision to have a text node for the title
value; in this regard, since changing the node's value does nothing, it
could probably be considered as a "bug" in those browsers, but certainly
not in regards of the reference.

Apart from altering document.title, there's another standard way to
change the title, though probably less supported: change the text
property of the TITLE element.

<URL:http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-79243169>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top