String.fromCharCode won't work with a variable?

Discussion in 'JavaScript' started by Kerosene, Apr 16, 2011.

  1. #1
    When I use String.fromCharCode to directly convert Unicode values to characters it works.

    THIS WORKS
    
    alert(String.fromCharCode(104,116,116,112,58,47,47,119,119,119,46,103,111,111,103,108,101,46,99,111,109));
     
    Code (markup):
    But if I want to put the Unicode values in a variable, then convert the variable to characters, it doesn't work.

    THIS DOESN'T WORK

    
    var mylink="104,116,116,112,58,47,47,119,119,119,46,103,111,111,103,108,101,46,99,111,109";
    alert(String.fromCharCode(mylink));
    
    Code (markup):
    Why? How can I make String.fromCharCode work with a variable?
     
    Kerosene, Apr 16, 2011 IP
  2. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #2
    
    var mylink="104,116,116,112,58,47,47,119,119,119,46,103,111,111,103,108,101,46,99,111,109";    
            var convertlist = mylink.split(",");
            finalstring = String.fromCharCode.apply(this,convertlist);
            alert(finalstring);
    
    Code (markup):
     
    Jan Novak, Apr 16, 2011 IP
    Kerosene likes this.
  3. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Jan has the solution. Basically, you entered text into a function that only reads numbers.
     
    Cash Nebula, Apr 16, 2011 IP
  4. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #4
    Thanks Jan, tested and working :D

    I spent about an hour trying to solve it. I knew the problem had something do to with String.fromCharCode only accepting numbers, but just couldn't get anything working.

    +REPZ
     
    Kerosene, Apr 16, 2011 IP