Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts

    wrong value being alerted

    This has done my head right in

    Code:
    <html>
    <head>
    <script>
    function echo_something(num)
    {
        alert(num);
        
    }
    </script>
    </head>
    <body>
        <input type="button" value="Click Me" onchange="echo_something(0000265)" />
    </body>
    </html>
    Why on earth does this alert me with 181.. Surely it should alert me with 265

    I need to figure this out for a project because i am passing zero filled values from my DB in to a javascript function. But i need the integer representation at the other end.

    Anyone know whats going on here?

    Edit:
    I think converting 181 to octal gives 265
    Last edited by timgolding; 06-07-2013 at 03:10 PM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    http://stackoverflow.com/questions/1...tarting-with-0
    https://developer.mozilla.org/en-US/...erals#Integers

    A numeric literal that starts with a 0 is treated as an octal number. So 13 from base 8 is 11 in base 10...

    Octal numeric literals have been deprecated, but still work if you are not in strict mode.

    (You didn't ask, but) A numeric literal that starts with 0x is treated as hexadecimal.

    More info at MDN.
    so this would work

    Code:
    <input type="button" value="Click Me" onclick="echo_something('0000265')" />
    also the attribute should be onclick, not onchange for a button

  • Users who have thanked DanInMa for this post:

    timgolding (06-07-2013)

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Thanks that works ill just put quotes around the argument. Works for me.
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by timgolding View Post
    Thanks that works ill just put quotes around the argument. Works for me.
    The better solution would be to update to using the latest version of JavaScript by adding "use strict"; to your function.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by felgall View Post
    The better solution would be to update to using the latest version of JavaScript by adding "use strict"; to your function.
    sadly, that won't fix it in many existing browsers...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by rnd me View Post
    sadly, that won't fix it in many existing browsers...
    only antiquated versions of JavaScript.

    It is still better to use the latest JavaScript even though there are a few things (such as octal) that you still need to add extra checks for in case your visitor is using an old version of IE.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Its not a problem this application is specifically built to run on firefox.
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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