SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Dec 2003
    Location
    South Korea
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    declaring variable types

    i have this variable:
    var x = parent.map.document.coordinates.x_coord.value

    map being an iframe name, coordinates being the form name in the iframe, and x_coord being an <input type="hidden" value="a number">

    Now, what i want to do is add lets say 20 to this variable and add it to the url. I tried doing this:

    parent.map.location = "file.php?x="+(x+20)+""

    However, i think it just treats the variable x as a string and just adds them together. So if variable x was 20, the url would turn up like:

    file.php?x=2020 instead of what i want it to be: file.php?x=40

    Is there any way to declare the variable an integer. I kno they have these functons for php but im not too sure about javascript.

    Any help is appreciated.

  2. #2
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't declare variable types in JS. In this situation the easiest workaround is not adding 20, but subtracting -20:
    Code:
    parent.map.location = "file.php?x="+(x- -20)

  3. #3
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In JavaScript you can convert a string value to a number with several different methods, Number(), parseInt(), or parseFloat(), depending on what you want. Assuming a string representing a number named str_num, one possible syntax would be:
    var new_num=Number(str_num) + 20;

    Characteristically, a number will be stored as a number if it is assigned without quotes, e.g. var x=20; or as a string if it is, e.g. var x="20"; The exception, as you have found, is that if you are applying the number to an existing string variable, automatic conversion will occur.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure about 'easiest' - but I'd recommend this:
    Code:
    var x = Number(parent.map.document.coordinates.x_coord.value);
    if (!isNaN(x))
        parent.map.location = 'file.php?x=' + String(x + 20);
    The negative-subtraction approach might work but it confuses your intent to anyone reading the code. Explicit coercion is a good habit to develop imo.
    ::: certified wild guess :::

  5. #5
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd guess every programmer with a little bit of experience in JS is aware of the problem the overloaded + operator poses and would recognize my construct?
    Furthermore, the first post states that x_coord is a hidden input which can only contain a number so your error checking seems excessive.

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd guess every programmer with a little bit of experience in JS is aware of the problem the overloaded + operator poses and would recognize my construct?
    Didn't say they wouldn't, just that if you can code it in a way that clearly indicates your intent, it's less likely to require that experience. I deal with experienced Java/C++/JSP/Perl practitioners all the time - and most of them don't know the specifics of JavaScript from a hole in the wall; as a money-maker, it ranks fairly low in the skillset repetoire.

    Not sure how a simple integrity check hurts anything. Data gets corrupted in the real world.
    ::: certified wild guess :::

  7. #7
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ok, I see your point. I still find your code a bit over the top but I commend your effort - considering the average script used on the web looks like it was written by someone who skimmed through the first two chapters of "JS For Dummies" we sure could use more code following your style.

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And yours. Always enjoy your posts, you sound like an actual professional. I record music professionally, webdev is an occasionally paying (>5 Yen) avocation.

    Just happen to be in overkill mode this afternoon; on another day I'd as soon skip the constructor coercion and do it your way...

    'Europe' is pretty mysterious; which corner are you inhabiting?
    ::: certified wild guess :::

  9. #9
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no professional interest in JavaScript, just a hobby. It's a bit weird, I feel I owe the language something because I hated it since its inception, thanks to all those idiots who couldn't think of anything better to do with it than popups, status bar scrollers and alerts complaining about my favourite browser. So i disabled JS only to be even more annoyed by all those sites which don't do anything without it.
    It wasn't before I heard that many of Mozilla's components are written in JS that I decided to take a second look and "discovered" that JS is actually a pretty nice language...
    I'm currently in Germany.

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interessant. Seen this?

    http://javascript.weblogsinc.com/
    ::: certified wild guess :::


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
  •