javascript malfunction in IE and firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahmoodn
    New Member
    • May 2009
    • 77

    #1

    javascript malfunction in IE and firefox

    I have written a javascript code but it works only in IE and chrome and not in firefox. I mean, when I click the button to call the function, in firefox I see no action (!)

    On the other hand the background image is displayed only in firefox and not in IE.

    What is the source of these inconsistencies ?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Browsers don't all behave the same, but it usually helps to code to standards where possible. Post your code, otherwise we'd have no idea what might be causing the problem.

    Comment

    • mahmoodn
      New Member
      • May 2009
      • 77

      #3
      I thought it is a general problem. Here is the code. Thanks,

      Code:
      <!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>
      <title>Untitled Document</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
      <style type="text/css">
      <!--
      .style2 {font-size: 18px}
      body {
      	background-image: url(background.jpg);
      }
      -->
      </style>
      </head>
      
      <body>
      <script language='javascript' type='text/javascript'>
      function calc() {
       <!-- main code -->
      
      </script>
      
      
      <form id="form1" name="form1" method="post" action="">
        <p>
          <label>
      
          <div align="left">
            <fieldset>
            <legend><span class="style2">Environment</span></legend>
            <div align="left">
              <input type="radio" name="Environment" value="1" id="Environment_0" >
              <span class="style2">Residential</span> </div>
            <div align="left"><input type="radio" name="Environment" value="2" id="Environment_1" >
              <span class="style2">Official</span></div>
            <div align="left"><input type="radio" name="Environment" value="3" id="Environment_2" >
              <span class="style2">Industrial</span></div>
      </fieldset>
            <p align="left" class="style2">Please insert the following information:</p>
      
      <label>
        <div align="center" class="style2">Velocity (m/s)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
          <select name="V" id="V">
            <option selected >Very low</option>
            <option>Low</option>
            <option>Medium</option>
            <option>High</option>
            <option>Very high</option>
          </select>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Garment Insulation (clo)
      <select name="C" id="C">
        <option selected="selected">Low</option>
        <option>Medium</option>
        <option>High</option>
      </select>
        </div>
        <span class="style2">
        </label>
        <label>
        </span>
      <div align="center" class="style2">Relative humidity (%)
         <input name="R" type="text" id="R" size="8" >
      &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Metabolic Heat Generation (met)
      <select name="M" id="M">
        <option selected="selected">Low</option>
        <option>Medium</option>
        <option>High</option>
      </select>
      </div>
      </label>
      <label></label>
        <label>
        
      <div align="left">
        <p align="center">
          <input name="C2" type="button" id="C2" value="Calculate" onclick="calc()" />
        </p>
      </div>
      </label>
        <label>
        <div align="left">
          <p align="center">Temperature (C)
            <input name="T" type="text" id="tem" size="10" maxlength="5" >
          </p>
      </div>
        </label>
      <label>
      
      <div align="left">
        <p align="center">
          <input name="E" type="submit" id="E" onclick="window.close()" value="Exit" >
        </p>
      </div>
      </label>
      <p>&nbsp;</p>
      <p> </p>
      </body>
      </html>
      Last edited by acoder; May 21 '09, 10:28 AM. Reason: Added [code] tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        If it's the calc() function that's not working, I'll need to see that. Can you post that using code tags please.

        Comment

        • mahmoodn
          New Member
          • May 2009
          • 77

          #5
          It is also lengthy. thanks,
          Code:
          	var k = 0;
          	if ( form1.Environment[0].checked ) {
          		k = 1;
          	}
          	else if ( form1.Environment[1].checked )
          		k = 2;
          	else if ( form1.Environment[2].checked )
          		k = 3;
          
          	var n = 0;
          
          	<!-- reading velocity combo box -->
          	var zz = form1.V.selectedIndex;
          	switch ( zz ) {
          		case 0:
          			var v = 0.1;
          			break;
          		case 1:
          			var v = 0.2;
          			break;
          		case 2:
          			var v = 0.3;
          			break;
          		case 3:
          			var v = 0.5;
          			break;
          		case 4:
          			var v = 1.5;
          			break;
          	}
          
          	var i = form1.M.selectedIndex;
          	var j = form1.C.selectedIndex;
          
          	switch ( k ) {
          		case 1:
          			switch ( i ) {
          				case 0:
          					var met = 0.7;
          					break;
          				case 1:
          					var met = 1.2;
          					break;
          				case 2:
          					var met = 2.4;
          					break;
          				case 0:
          					var met = 0.7;
          					break;
          			}
          			switch ( j ) {
          				case 0:
          					var clo = 0.38;
          					break;
          				case 1:
          					var clo = 0.5;
          					break;
          				case 2:
          					var clo = 0.9;
          					break;
          			}
          			break;
          		case 2:
          			switch ( i ) {
          				case 0:
          					var met = 1.0;
          					break;
          				case 1:
          					var met = 1.4;
          					break;
          				case 2:
          					var met = 2;
          					break;
          			}
          			switch ( j ) {
          				case 0:
          					var clo = 0.7;
          					break;
          				case 1:
          					var clo = 1.0;
          					break;
          				case 2:
          					var clo = 2;
          					break;
          			}
          			break;
          		case 3:
          			switch ( i ) {
          				case 0:
          					var met = 2.0;
          					break;
          				case 1:
          					var met = 3.0;
          					break;
          				case 2:
          					alert ( "wrong" );
          					break;
          			}
          			switch ( j ) {
          				case 0:
          					var clo = 0.7;
          					break;
          				case 1:
          					var clo = 1.4;
          					break;
          				case 2:
          					var clo = 2.5;
          					break;
          			}
          			break;
          	}
          
          	var hc = 12.1 * Math.pow(v, 0.5);
          	var Rcl = clo * 0.155;
          	var M = met * 58.1;
          	var W = 0.05 * M;
          	if ( clo > 0.5 )
             		var fcl = 1.05 + 0.1 * clo;
          	else
             		var fcl = 1.0 + 0.2 * clo;
          
          	var hum = parseFloat(form1.R.value);
          	var Tcl1 = -10.0;
          	var Tcl2 = 40.0;
          	var T = 5.0;
          	var TemOpt = 5.0;
          	var dif = 100;
          
          	for ( z = 1; z <= 1000; z++ ) {
          	    var Pt = Math.exp(16.6536 - 4030.183 / (T + 235.0));
          	    var Pa = hum * 10 * Pt;
              	var Tcl = root(Tcl1, Tcl2, M, W, fcl, hc, Rcl, T);
              	var l = (M - W)
              	var PMV = (0.303 * Math.exp(-0.036 * M) + 0.028) * l;
              	if (Math.abs(Math.abs(PMV) - 0.5) < dif) {
                 		var TemOpt = T;
                 		dif = Math.abs(Math.abs(PMV) - 0.5);
              	}
             		T = T + 0.03;
          
          	}
          	form1.tem.value = TemOpt;
          }
          
          function root( Tcl1, Tcl2, M, W, fcl, hc, Rcl, T ) {
          	while (Math.abs(f(M, W, fcl, hc, Rcl, Tcl1, T) >= 0.001 )) {
            		var Tcl3 = Tcl2 - f(M, W, fcl, hc, Rcl, Tcl2, T) - f(M, W, fcl, hc, Rcl, Tcl1, T);
          
          	if (f(M, W, fcl, hc, Rcl, Tcl3, T) * f(M, W, fcl, hc, Rcl, Tcl1, T) < 0.0)
          		var Tcl1 = Tcl3;
            	else
              	var Tcl2 = Tcl3;
          	}
          	return Tcl3;
          }
          
          function f(M, W, fcl, hc, Rcl, Tcl, T) {
          	var x = 35.7 - 0.028 * (M - W) - Rcl * (0.0000000396 * fcl * (Math.pow(Tcl + 273, 4) - Math.pow(T + 273, 4)) + fcl * hc * (Tcl - T)) - Tcl;
          	return x;
          }

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            I've not checked the rest of it, but the first problem I notice is the reference to form1 in the global scope. That is incorrect. Either use
            Code:
            document.forms["form1"]
            // or
            document.form1
            // or
            document.getElementById("form1")

            Comment

            • mahmoodn
              New Member
              • May 2009
              • 77

              #7
              Thanks, That worked for clac(), but the "exit" button reset the form. It does not show a window to confirm for close.
              The original Exit function is:
              Code:
              <input name="E" type="submit" id="E" onclick="window.close()" value="Exit" >
              With your cue, it has to be document.form1. window.close(), but that does not work either.

              Also what about the problem of backgound image. It is shown in firefox but not in IE (!)

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                document.form1. window.close() would be incorrect. window.close() only works on windows opened using JavaScript. If this is in the original browser window, it won't work. Use it in a pop-up window opened using window.open().

                Comment

                • mahmoodn
                  New Member
                  • May 2009
                  • 77

                  #9
                  Sorry, did not understand what you said... :(

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    When you open the browser and visit a web page, that is the browser you opened. Now, on that web page, when you click a button, it opens a new window. This window has been opened using JavaScript, so can be closed using JavaScript (window.close() ) whereas the previous browser window cannot.

                    Comment

                    • mahmoodn
                      New Member
                      • May 2009
                      • 77

                      #11
                      yes. In this site, I saw a solution for that. There should be a manual close function and in that, first the window has to be opened and then call the close function. An interesting thing is that "dom.allow_scri pts_to_close_wi ndows" in "about:conf ig" (firefox) has to be 'true'. Thanks for the reference.


                      How about the background image? it works in firefox but not in IE (!)

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Yes, I've seen that hack, but it doesn't work in all browsers (for obvious reasons).

                        As for the background image, it's a CSS problem, so ask in the HTML/CSS forum.

                        Comment

                        • mahmoodn
                          New Member
                          • May 2009
                          • 77

                          #13
                          Is it possible to change a value in about:config (firefox) automatically? I mean when the page is loaded, with a javascript function I change "dom.allow_scri pts_to_close_wi ndows". If it is not possible, then I have to write a notice at the end of the page and alert the user that "exit" function may not work in firefox

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            I doubt that. it would be too much a security issue. (would you like a website changing your browser settings?)

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Why do you even need the Exit function? It'd make sense if you'd opened a window, but in the original browser window, there's no need to close it.

                              Comment

                              Working...