Java script entities and firefox

Discussion in 'JavaScript' started by ahmed-hitec, Aug 10, 2007.

  1. #1
    Hi,
    i am new in JavaScript and have a small problem
    i am currently reading the "JavaScript complete reference" e-book, and the following example, about JavaScript entities doesn't work on Firefox 2.0.06
    it works fine on ie7. the code is as follows :

    <head>
    <script language="javascript1.5">
    var Bordersize=5;
    var tablecellcolor="#ff0000";
    var alignment = "center";

    function setImage (){
    var today = new Date();
    var hours = today.getHours();
    if ((hours >8) && (hours < 18))
    return 'Sunset.jpg'
    else
    return 'moon.jpg';
    }
    </script>
    </head>
    <body>
    <table border="&{Bordersize };" align="&{alignment };">
    <tr>
    <td bgcolor="&{tablecellcolor };">Java script entities</td>
    </tr>
    </table>
    In the sky now : <img src="&{setImage() };" />
    </body>

    The entities in red should reefer to the variables declared and initialized in the head tag, and it does so but only in ie7,

    I need your help, does Firefox is not fully compatible with JavaScript, I don't think so !
     
    ahmed-hitec, Aug 10, 2007 IP
  2. Drag Racer

    Drag Racer Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    no, not a problem with Firefox, its the way your trying to access the variable is non-standard. Variable access is unavailable outside of the script.

    What I would do is set a unique 'ID' to each html tag you want to change. Then access the tag from your script with 'getElementById()'. Then attach an 'onLoad' to the body tag so the script runs after the document loads. This step is needed due to tags will not be available and generate javascript errors if the script runs before the tag is loaded.

    example...
    <html>
    <head>
    <script type="text/javascript">
    function setImage(){
     var img = document.getElementById('img1');
     var today = new Date();
     var hours = today.getHours();
     if ((hours >8) && (hours < 18))
      img.src = 'Sunset.jpg'
     else
      img.src = 'moon.jpg';
    }
    </script>
    </head>
    <body onLoad="setImage();">
    <img src="" id="img1">
    </body>
    </html>
    Code (markup):
     
    Drag Racer, Aug 10, 2007 IP
  3. paullm

    paullm Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    let me resurrect the thread. I'm reading a book "Advanced JavaScript 3rd Edition" written by Chuck Eastom. And it is told there that javascript entities exist and we can use them. He has an example there:

    <HTML>
    <HEAD>
    <TITLE>JavaScript Entities</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- hide content from old browsers
    var fontSize = "+4"
    var fontColor = "red"
    // end hiding content-->
    </SCRIPT>
    </HEAD>
    <BODY>
    <FONT COLOR="&{fontColor};" SIZE="&{fontSize};">
    Flexible attributes with JavaScript entities
    </FONT>
    </BODY>
    </HTML>


    and for gods sake, this example doesn't work in any browser. I tested it in IE8, FF3.5, Opera 9.64 and Safari. In all browsers the example didn't work.
    Two questions:
    1. Are javascript entities real?
    2. Should I flush the book and buy another one?
    thanks.
     
    paullm, Oct 15, 2009 IP