[Javascript] Answer (maybe) to: Bizarre IE behaviour.

Shawn Milo ShawnMilo at runbox.com
Thu Aug 12 12:27:17 CDT 2004


I found this to be really, really interesting.  So I did a little experiment:

  <script type="text/javascript">
   var x;
   var someNum;
   
   for (x=0;x<10;x++){
      someNum = '0' + x
      alert('string:' + someNum + ', number: ' + parseInt(someNum));   
   }

  </script>

I noticed that it happened not just to the 9, but to the 8, as well.  So I tried it up to 20, and got very odd results:  parseInt('010') = 8, parseInt('011') = 9, and so on.  That gave me a clue.  

It appears that the leading zero is prompting JS in IE to convert the string as though it were an octal or hex value or something like that. That's as far as I took the research.

As a note of interest, I just upgraded my work machine to XP SP2, so I probably have the newest version of IE6 available, and this behaviour is still here.

Shawn








> This is probably me being fantastically short sighted, but I've noticed
> behaviour in IE this afternoon which was wholey unexpected, and also plain
> wrong ("so tell us something new", say the open source fraternity).
> 
> This is the situation.  I have a very simple page:
> 
> ----------------------------
> <html>
>  <body>
>   <script>
>    var strDay = '09';
>    var strMonth = '06';
>    var strYear = '2004';
>    alert (strDay);
>    alert (strMonth);
>    alert (strYear);
> 
>    var intDay = parseInt (strDay);
>    var intMonth = parseInt (strMonth);
>    var intYear = parseInt (strYear);
>    alert (intDay);
>    alert (intMonth);
>    alert (intYear);
> 
>   </script>
>  </body>
> </html>
> --------------------
> 
> When it loads I would expect to see six consecutive alert boxes containing,
> in order, the following strings: "09", "06", "2004", "9", "6", "2004".
> 
> However, in IE6/Win (on two separate machines in my office) the fourth alert
> box contains "0".
> 
> Does anyone else see this behaviour?  Tell me I'm not going mad ...
> 
> Oh - I've tested it on Opera (the only other browser I have immediate access
> to without installing another) and it works exactly as I would expect it to.
> 
> Matt
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> 



More information about the Javascript mailing list