JavaScript ‘Octal’pus – It’s a weird title, isn’t it!? No, no I am not going to tell any sci-fi story or something like that! It’s just a documentation of the JavaScript issue, that I faced yesterday.
In my web application, I had to show a value from the database and the I’ve to pass this value to a JavaScript function in my page. The database field is of type String and has values like ‘01004′ (with leading zeros). So, when I passed the value 01004 (without quotes), the actual value I received in JavaScript function is 516. I was clueless about how 01004 changed itself into 516.
So I posted my query to Stackoverflow and got the answer immediately. The actual problem is that I am passing 01004 without quotes, i.e. as a numeric and the leading zero makes the JavaScript to think 01104 as a Octal number, which is 516. This is how 0114 is converted to 516.
I just fixed it by removing the leading zero from my Servlet logic and then passing the number as 1004 so that I will always receive what I passed. Other fix suggested by Stackoverflow folks is to pass 01004 inside quotes and inside the JavaScript convert it back to Integer using parseInt('01004', 10). But the second option is like double the work., so I opted the first option.
If small mistakes, like this one, not caught properly, then they will catch you like an octopus.
{ 1 comment }
The amount of time that will be wasted is just too much, which obviously impact our productivity badly!
Comments on this entry are closed.