Why does javascript not work on Google Chrome?

advertisements

I've been facing a problem with javascript into JS page and since I'm a newbie on it I can't figure out why this code works on Firefox but not on Google Chrome. I'd be very thankful if someone can help me with this problem.

<script type="text/javascript">

function showFC(form) {

    var selElem = document.getElementById(form);
    var selIndex  = selElem.selectedIndex;

    document.form1.S4.options[0] = new Option("C=1",1);
    if(selIndex==0) {
        for(var i=0;i<10;i++) {
            var val = (i+1)+0.5;
            document.form1.S4.options[i+1] = new Option(val,val);
        }
    }
}
</script>

It doesn't create dynamically elements into the select when it clicked by a button.

Here my html code:

<form name="form1" method="post" action="ComputeResult">
     ... other things
     ....
     ....
   <select name="S4" id="S4" onclick="showFC('S4')">
      <option value="1">FC</option>
      <option value=""></option>
   </select>
</form>


In JavaScript, you have two ways of declaring a function. Either you declare it like this:

function f() {}

or

var f = function(){}

The difference between the two is that the first is defined at run time, the second one at parse-time (more details here: var functionName = function() {} vs function functionName() {}).

Most probably what happened in your case, is that your function is declared after your HTML is read, and so the function does not exist. So:

window.showFC = function(){}
showFC = function(){}

should both work.