How do I change the document title?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Trahan

    How do I change the document title?

    (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.create Element("title" );
    var newtext = document.create TextNode("MyTit le"+(dirty?"*": ""));
    newtitlenode.ap pendChild(newte xt);
    var headnode = document.getEle mentsByTagName( "head").item(0) ;
    var oldtitlenode = document.getEle mentsByTagName( "title").item(0 );
    headnode.replac eChild(newtitle node,oldtitleno de);
    headnode = document.getEle mentsByTagName( "head").item(0) ;

    How can I get the changed title to display?

    I'm running NN 7.1.

  • McKirahan

    #2
    Re: How do I change the document title?

    "Richard Trahan" <rtrahan@optonl ine.net> wrote in message
    news:DVm2d.1746 $Of4.2566770@ne ws4.srv.hcvlny. cv.net...[color=blue]
    > (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.create Element("title" );
    > var newtext = document.create TextNode("MyTit le"+(dirty?"*": ""));
    > newtitlenode.ap pendChild(newte xt);
    > var headnode = document.getEle mentsByTagName( "head").item(0) ;
    > var oldtitlenode = document.getEle mentsByTagName( "title").item(0 );
    > headnode.replac eChild(newtitle node,oldtitleno de);
    > headnode = document.getEle mentsByTagName( "head").item(0) ;
    >
    > How can I get the changed title to display?
    >
    > I'm running NN 7.1.
    >[/color]

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


    Comment

    • Richard Trahan

      #3
      Re: How do I change the document title?

      McKirahan wrote:

      (snip)

      Yes, of course! That works, thank you.

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

      Comment

      • RobG

        #4
        Re: How do I change the document title?

        Richard Trahan wrote:
        [color=blue]
        > McKirahan wrote:
        > Yes, of course! That works, thank you.
        > In the interest of higher education, what was wrong with my node method?[/color]

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

        var x = document.getEle mentsByTagName( "title").item(0 );
        alert('The ' + x.nodeName
        + ' now has value: '
        + x.firstChild.no deValue
        + '\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.

        Comment

        • Yann-Erwan Perio

          #5
          Re: How do I change the document title?

          Richard Trahan wrote:

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

          According to the DOM/HTML specification the HTMLTitleElemen t 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>

          Comment

          Working...