SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript function doesn't work in Mozilla Firefox

    I've been wrestling with a JavaScript conversion engine that I put online at

    http://northamerica.geoworld.org/unitconversion2.php

    only to discover that it works just fine - but only in Internet Explorer! I asked about it on a Mozilla forum, and this the response I got:

    This javascript only works in IE. It fails in Firefox, Mozilla, K-Meleon, Netscape 4.8 and Opera 7.23.

    The javasscrip console shows two errors:

    Error: awin has no properties
    Source File: http://www.burstnet.com/cgi-bin/ads/...ETURN-CODE/JS/
    Line: 4

    Error: sel1 is not defined
    Source File: http://northamerica.geoworld.org/unitconversion2.php
    Line: 8

    Does anyone have any idea how to fix this? I know virtually nothing about JavaScript except how to implement JS functions others have written on my web pages.

    Thanks!

  2. #2
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this sheds more light on the problem:

    "IE only, huh? I like that! Most of what I make seems to be IE only, many intentionally, not this one though,

    "The first error appears to be a server error, that cannot be controlled by be at least.

    "sel1 ... the name of the form and the first part of the variable. Maybe Mozilla doesn't support that. Maybe you should you getElementById. for the select box rather than the way we did it.

    "Then again, notice my avatar, image to the left. I hate Mozilla so it doesn't make a big difference."

    Sheez.

    I guess the next step is to find out if Mozilla supports "sel1." Anyone here know about that? And if it doesn't support it, can I simply do a search and replace, replacing sel1 with something else, or is it a more complicated fix than that?"

  3. #3
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can you post the function here so that we can look why is it IE only.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure. There's no PHP involved, but I'll post it between PHP tags, because I've been told that will yield color-coded text.

    PHP Code:
    <html>
    <
    head>
    <
    title>Unit Converter</title>
    <
    script type="text/javascript">

      function 
    convertVals() {
        
    // retrieve values and types of and for conversion
        
    var sel1_val1 parseFloat(document.sel1.conv[sel1.conv.selectedIndex].value)
        var 
    sel1_val2 document.sel1.conv[sel1.conv.selectedIndex].value2.toLowerCase()
        var 
    sel2_val1 parseFloat(document.sel2.conv[sel2.conv.selectedIndex].value)
        var 
    sel2_val2 document.sel2.conv[sel2.conv.selectedIndex].value2.toLowerCase()
        var 
    toConvert parseFloat(document.getElementById('amt').value)

        var 
    display ""

        
    if(sel1_val2 == sel2_val2 && sel1_val2 != "sep" && sel1_val2 != "temp" && toConvert == parseFloat(toConvert)) {
          
    // if same type, convert value
          
    display = ((toConvert sel1_val1) / sel2_val1).toFixed(8)
        }
        else if(
    sel1_val2 == sel2_val2 && sel1_val2 == "temp" && toConvert == parseFloat(toConvert)) {
          if(
    sel1_val1 == 100 && sel2_val1 == 212) {
             
    display = ((toConvert 5) + 32).toFixed(8)
          }
          else if(
    sel1_val1 == 212 && sel2_val1 == 100) {
             
    display = ((toConvert 32) * 9).toFixed(8)
          }
          else {
             
    display toConvert.toFixed(8)
          }
        }
        while(
    display.lastIndexOf('0') == display.length && display.length 0) {
          
    display display.substr(0,display.length 1)
        }
        if(
    display.lastIndexOf('.') == display.length 1) {
          
    display display.substr(0,display.length 1)
        }
        
    document.getElementById('amt2').value display

        
    if(display != "") {
          for(var 
    i=0;i<weights.length;i++) {
            if(
    sel1_val1 == weights[i] && sel1_val2 == types[i].toLowerCase()) {
              var 
    firstNum i
              
    break;
            }
          }

          for(var 
    i=0;i<weights.length;i++) {
            if(
    sel2_val1 == weights[i] && sel2_val2 == types[i].toLowerCase()) {
              var 
    secondNum i
              
    break;
            }
          }

          
    history1[history1.length] = toConvert
          history2
    [history2.length] = display
          history3
    [history3.length] = secondNum
          history4
    [history4.length] = firstNum
          total 
    history1.length 1
        
    }
      }

      var 
    types = new Array
      var 
    weights = new Array
      var 
    values = new Array

      function 
    setUpBox() {
        
    // there must be the same number in each array to set up box correctly!
     
        // enter the value for each option. This is what displays in the select box
        
    values = new Array("Select A Unit","-----------------","Millimeters","Centimeters","Inches","Decimeters","Meters","Feet","Decameters","Hectometers","Kilometers","Miles","-----------------","Square Miles","Square Kilometers","-----------------","Dollars","Cents","-----------------","Grams","Ounces","Pounds","Tons","Kilograms","-----------------","Celcius","Fahrenheit")

        
    // enter the conversion factors for each of the values above
        
    weights = new Array(0,0,.001,.01,.0254,.1,1,.3048,10,100,1000,1609,0,0.386,1,0,1,.01,0,.00205,0.0625,1,2000,2.205,0,100,212)

        
    // types that are used as separators MUST have name: sep
        
    types = new Array("sep","sep","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","sep","sqrUnits","sqrUnits","sep","currency","currency","sep","weight","weight","weight","weight","weight","sep","temp","temp")

        var 
    resultStr '<select name="conv" size=5 onchange="convertVals()">'
        
    resultStr += '<option value="'weights[0] +'" value2="'types[0] +'" selected>'values[0]

        for(var 
    i=1;i<values.length;i++) {
          
    resultStr += '<option value="'weights[i] +'" value2="'types[i] +'">'values[i]
        }

        
    resultStr += '</select>'
        
    document.write(resultStr)
      }

      var 
    history1 = new Array
      var 
    history2 = new Array
      var 
    history3 = new Array
      var 
    history4 = new Array
      var 
    total history1.length

      
    function navHistory(num) {
        if(
    history1[total num] != undefined) {
          
    document.getElementById('amt').value history1[total num]
          
    document.getElementById('amt2').value history2[total num]
          
    document.sel2.conv.selectedIndex history3[total num]
          
    document.sel1.conv.selectedIndex history4[total num]
          
    total -= num
        
    }
      }

      function 
    clearHist() {
        var 
    temp history1.splice(0,history1.length-1)
        var 
    temp2 history2.splice(0,history2.length-1)
        var 
    temp3 history3.splice(0,history3.length-1)
        var 
    temp4 history4.splice(0,history4.length-1)
        
    total 0
      
    }

    </script>
    </head>

    <body><center><b><big>Unit Converter:</big></b></center><BR>
    <table VALIGN="top" align="center" border="1" bordercolor="black" cellpadding=8 cellspacing=0>
      <tr><td colspan="2"><center>
        Enter amount to convert: <input type="text" id="amt" onclick="if(this.value==history1[history1.length-1]) this.value='';">
      </center></td></tr>
      <tr>
        <td><center>
          <form name="sel1">From Units:<BR><BR>
            <script type="text/javascript"> setUpBox() </script>        
          </form>
        </center></td>
        <td><center>
          <form name="sel2">To Units:<BR><BR>
            <script type="text/javascript"> setUpBox() </script>
          </form>
        </center></td>
      </tr>
      <tr><td colspan="2"><center>
        <input type="button" value="Convert >>" onclick="document.getElementById('amt').focus();convertVals()"> <input type="text" id="amt2">
      </center></td></tr>
      <tr><td colspan="2"><center>
        <input type="button" value="<< Last" onclick="document.getElementById('amt').focus();navHistory(1)">&nbsp&nbsp&nbsp<input type="button" value="Next >>" onclick="document.getElementById('amt').focus();navHistory(-1)">&nbsp&nbsp&nbsp<input type="button" value="Clear History" onclick="document.getElementById('amt').focus();clearHist()">
      </center></td></tr>
    </table>
    </body>
    </html> 

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second one is easy:
    Code:
    var sel1_val1 = parseFloat(document.sel1.conv[sel1.conv.selectedIndex].value)
    You need to specify the full hierarchial structure - excepting window - each time you use 'sel1', not just for the first reference:
    Code:
    parseFloat(document.sel1.conv[document.sel1.conv.selectedIndex].value)
    A little more sano:
    Code:
    var conv1 = document.sel1.conv;
    var conv2 = document.sel2.conv;
    var sel1_val1 = parseFloat(conv1[conv1.selectedIndex].value);
    var sel1_val2 = conv1[conv1.selectedIndex].value2.toLowerCase();
    var sel2_val1 = parseFloat(conv2[conv2.selectedIndex].value);
    var sel2_val2 = conv2[conv2.selectedIndex].value2.toLowerCase();
    Can't say I've seen this use of a second, custom value attribute for an option - but, if it works...

    The other error is BurstNET related, unclear why it's there.
    ::: certified wild guess :::

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips. I think I've got it squared away now.

  7. #7
    SitePoint Member ronaldo30's Avatar
    Join Date
    Apr 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the same problem.
    You can put code rebuilt, not go for that.

    Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •