Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

javascript parseInt is broken

javascript parseInt is broken
I was debugging some strange errors in a date conversion function I was writing, and I stumbled upon something that amazed me... a strange bug in parseInt
>>>parseInt('06')
6
>>>parseInt('07')
7
>>>parseInt('08')
0
>>>parseInt('09')
0
>>>parseFloat('08')
8
>>>parseFloat('09')
9
 
Clearly for the strings '08' and '09' parseInt fails to return the right value. One workaround is easy, use parseFloat.
Why does this happen and where should I report this? I find it extremely odd that it happens in both Firefox and Internet Explorer 6 & 7; there must be some explanation right?
You can test it out for yourself using the FireBug javascript console), or by using one of these two links
alert(parseInt('07')+' == 7 ?')
alert(parseInt('08')+' == 8 ?')
Ok, Ok, so now that I explained how you would expect it to work (and clearly how I expected it to work) I'm going to answer my own question (yes I hate it when I do that too)
The problem is with how parseInt guesses the base of your number. Read the parseInt spec. Instead of always defaulting to base 10, it tries to guess, and if the first character is '0' it thinks you want to parse as an octal number, and if it starts with '0x' it thinks you want hexadecimal.
So, the solution is either to use parseFloat or to always specify your base.
Defaults that change on their own can't be trusted.
>>>parseInt('08',10)
8
 


Ïà¹ØÎĵµ£º

JavaScript Êó±êÍÏק½øÐпìËÙ²úÆ··ÖÀàЧ¹û

´úÂëʾÀý
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ÎÞ±êÌâÎĵµ</title>
</head>
<mce:style><!--
.man{ float:left; background:#9af; margin:3px; padding:3px; cursor:default}
--></mce:style>< ......

flex ºÍjavascript»¥Ïàµ÷ÓÃ

flex ºÍjavascript»¥Ïàµ÷Óá£
Ò»¡¢flex µ÷ÓÃjavascript
  Ö»ÐèʹÓþ²Ì¬µÄExternalInterface.call¾Í¿ÉÒÔÁË¡£
  call ·½·¨ÖÁÉÙÒªÇóÒ»¸ö²ÎÊý£¬¾ÍËùµ÷ÓöÊýµÄÃû³Æ¡£
  Ò²¿ÉÒÔ´«µÝ²ÎÊý¡£
  È磺
    ExternalInterface.call("alert","hello");
   "hello"¾ÍÊÇ×÷Ϊalertµ ......

JavaScriptÅжÏä¯ÀÀÆ÷ÀàÐͼ°°æ±¾

      ͨ¹ý·ÖÎö¸÷Ààä¯ÀÀÆ÷µÄuserAgentÐÅÏ¢£¬²»Äѵóö·Ö±æ¸÷Ààä¯ÀÀÆ÷¼°Æä°æ±¾µÄÕýÔò±í´ïʽ¡£¶øÇÒ£¬¶Ôä¯ÀÀÆ÷ÀàÐ͵ÄÅжϺͰ汾µÄÅжÏÍêÈ«¿ÉÒÔºÏΪһÌåµØ½øÐС£ÓÚÊÇ£¬ÎÒÃÇ¿ÉÒÔд³öÏÂÃæµÄ´úÂ룺
    <script type="text/javascript">
       ......

JavascriptʵÏÖÈÕÆÚʱ¼äµÄµ¹¼Æʱ

<html>
<body>
<SCRIPT type="text/javascript">
<!--
var target=[]
var time_id=[]
function ShowDateTime(){
    setTimeout("ShowDateTime()", 1000);
for (var i=0,j=target.length;i<j;i++)
{
    var today=new Date();
    timeo ......

JSP+JavaScript´òÔì¶þ¼¶¼¶ÁªÏÂÀ­²Ëµ¥

JSP+JavaScrip
t´òÔì¶þ¼¶¼¶ÁªÏÂÀ­²Ëµ¥£º
(¸öÈ˲©¿Í£ºwww.duduct.com)
class(Ò»¼¶À¸Ä¿ÐÅÏ¢£©£ºclassId(×Ô¶¯±àºÅ£©£¬className£¨À¸Ä¿Ãû³Æ£©,
Nclass£¨¶þ¼¶À¸Ä¿ÐÅÏ¢£©,
NclassId£¨×Ô¶¯±àºÅ£©£¬NclassName(À¸Ä¿Ãû³Æ£©£¬parentId£¨Ò»¼¶À¸Ä¿id£¬Óëclass±íÖеÄclassId¹ØÁª£©
......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ