getElementbyId Firefox v IE

Discussion in 'Javascript' started by zippy, Jan 25, 2006.

  1. zippy

    zippy Guest

    My code works fine in IE but not FF.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs"
    Inherits="test" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    <script>
    function window_onload()
    {
    runTime();
    setInterval(runTime, 60000);

    // todo: apply png transparent style for ie < 7.0
    //
    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=Images/WebDesktopIconRound.png)
    }

    function runTime()
    {
    var rightPart = document.getElementById("dvRightTemplate");
    var time = new Date();
    var hour = time.getHours();
    var minute = time.getMinutes();
    var second = time.getSeconds();
    var temp = "";
    temp += ((hour < 10) ? "0" : "")+ hour +((minute < 10) ?
    ":0" : ":") + minute;
    rightPart.innerText = temp;
    }

    </script>
    </head>

    <body id="body" runat="server" o>
    <form id="form1" runat="server">
    <div>
    <div id="dvRightTemplate" align="right" style="padding-right:
    10px; font-size: 8pt;
    width: 80px; font-family: Tahoma; color:black">
    <!-- Notes: Always specify width attribute for
    proper rendering in Mozilla -->
    00:00</div>
    </div>
    </form>

    </body>
    </html>
     
    zippy, Jan 25, 2006
    #1
    1. Advertising

  2. zippy

    bne Guest

    zippy wrote:

    > My code works fine in IE but not FF.
    >
    > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs"
    > Inherits="test" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title>Untitled Page</title>
    > <script>
    > function window_onload()
    > {
    > runTime();
    > setInterval(runTime, 60000);
    >
    > // todo: apply png transparent style for ie < 7.0
    > //
    > filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=Images/WebDesktopIconRound.png)
    > }
    >
    > function runTime()
    > {
    > var rightPart = document.getElementById("dvRightTemplate");
    > var time = new Date();
    > var hour = time.getHours();
    > var minute = time.getMinutes();
    > var second = time.getSeconds();
    > var temp = "";
    > temp += ((hour < 10) ? "0" : "")+ hour +((minute < 10) ?
    > ":0" : ":") + minute;
    > rightPart.innerText = temp;
    > }
    >
    > </script>
    > </head>
    >
    > <body id="body" runat="server" o>
    > <form id="form1" runat="server">
    > <div>
    > <div id="dvRightTemplate" align="right" style="padding-right:
    > 10px; font-size: 8pt;
    > width: 80px; font-family: Tahoma; color:black">
    > <!-- Notes: Always specify width attribute for
    > proper rendering in Mozilla -->
    > 00:00</div>
    > </div>
    > </form>
    >
    > </body>
    > </html>


    Hi

    innerText is specific to the IE DOM use innerHTML instead.

    ben
     
    bne, Jan 25, 2006
    #2
    1. Advertising

  3. zippy

    zippy Guest

    Thanks! How annoying!
     
    zippy, Jan 25, 2006
    #3
  4. zippy

    RobG Guest

    bne wrote:
    [...]
    >
    > innerText is specific to the IE DOM use innerHTML instead.



    The DOM equivalent to IE's innerText property is textContent. Since
    it's part of DOM 3, not all browsers support it but newer Gecko browsers
    do (and probably others). A reasonable emulation is:

    function getTextContent(el)
    {
    if (el.textContent) return el.textContent;
    if (el.innerText) return (el.innerText);

    var cNode, cNodes = el.childNodes;
    var txt = '';
    for (var i=0, len=cNodes.length; i<len; ++i){
    cNode = cNodes;
    if (1 == cNode.nodeType) {
    txt += getTextContent(cNode);
    }
    if (3 == cNode.nodeType){
    txt += cNode.data;
    }
    }
    return txt;
    }


    Newlines may be inserted inconsistently in different browsers. Support
    for other node types may be required.


    --
    Rob
     
    RobG, Jan 27, 2006
    #4
  5. zippy

    RobG Guest

    one man army wrote:
    [...]
    > also, a search in the newsgroup showed that document.getElementById()
    > is not always supported. I don't know how far abck that is going. But


    The FAQ offers a dynWrite function, but there is an alternative offered
    here:

    <URL: http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html#alDynWr >


    I like option 3, it is probably suitable in most cases - but read the
    accompanying notes.


    --
    Rob
     
    RobG, Jan 27, 2006
    #5
  6. RobG wrote:

    > bne wrote:
    > [...]
    >> innerText is specific to the IE DOM use innerHTML instead.

    >
    > The DOM equivalent to IE's innerText property is textContent.


    Unfortunately, I do not think it is. IE trims whitespace text nodes in its
    DOM, so it is likely that it also trims it from the value of `innerText'.
    I am not sure about that, though, since I have not IE to test with.

    > Since it's part of DOM 3, not all browsers support it but newer Gecko
    > browsers do (and probably others). A reasonable emulation is:
    >
    > function getTextContent(el)
    > {
    > if (el.textContent) return el.textContent;
    > if (el.innerText) return (el.innerText);
    > [...]


    Neither will return if the element content is (considered) empty, and so
    force the following code to execute. Evaluating the `typeof' operation is
    more reliable here.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 15, 2006
    #6
    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. TR
    Replies:
    5
    Views:
    131
    Richard Cornford
    May 19, 2004
  2. Hartmut J?ger

    Firefox and getElementById

    Hartmut J?ger, Nov 27, 2004, in forum: Javascript
    Replies:
    6
    Views:
    150
    Michael Winter
    Nov 28, 2004
  3. Dave Hammond
    Replies:
    9
    Views:
    113
    Evertjan.
    Feb 16, 2005
  4. Muffinman
    Replies:
    1
    Views:
    140
    Zifud
    Mar 31, 2005
  5. Derek Erb
    Replies:
    5
    Views:
    190
Loading...

Share This Page