How do I change the document title?

Discussion in 'Javascript' started by Richard Trahan, Sep 16, 2004.

  1. (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.
     
    Richard Trahan, Sep 16, 2004
    #1
    1. Advertising

  2. Richard Trahan

    McKirahan Guest

    "Richard Trahan" <> wrote in message
    news:DVm2d.1746$...
    > (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>
     
    McKirahan, Sep 16, 2004
    #2
    1. Advertising

  3. McKirahan wrote:

    (snip)

    Yes, of course! That works, thank you.

    In the interest of higher education, what was wrong with my node method?
     
    Richard Trahan, Sep 16, 2004
    #3
  4. Richard Trahan

    RobG Guest

    Richard Trahan wrote:

    > 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.
     
    RobG, Sep 17, 2004
    #4
  5. 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>
     
    Yann-Erwan Perio, Sep 17, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    500
    Steve C. Orr, MCSD
    Aug 10, 2003
  2. Cowboy \(Gregory A. Beamer\) [MVP]

    Re: Change document.title

    Cowboy \(Gregory A. Beamer\) [MVP], Jul 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    710
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 7, 2004
  3. Replies:
    0
    Views:
    580
  4. Replies:
    1
    Views:
    598
    David
    Apr 24, 2007
  5. soonic
    Replies:
    11
    Views:
    1,855
    soonic
    Jul 27, 2011
Loading...

Share This Page