Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Passing Strings with commas to a javascript method

Options
  • 11-08-2010 8:36pm
    #1
    Registered Users Posts: 46


    Hi people,

    This is probably a quicky for someone but its wrecking my head - I am trying to pass 2 strings which contain commas to a javascript method within a jsp but it fails as the javascript method thinks that the words in the string before and after the comma are seperate variables

    here is what I am trying to do in a simplified form:
     <script type="text/javascript">
    function initialize(str1,str2) {
         console.log(str1);
         console.log(str2);
    }
    </script>
    <%
    String str1 = "hello, world";
    String str2 = "stupid, commas";
    %>
    
    <body onload="initialize(<%=str1%>,<%=str2%>)">
    <p>&nbsp;</p>
    </body>
    
    

    The error that firebug gives me is :

    hello is not defined


    Hopefully someone has experienced this before and can point me in the right direction.

    Cheers,
    Patrick.


Comments

  • Registered Users Posts: 46 Paddy GT


    Also before someone mentions that I could just escape the commas unfortunately I need the commas in the strings as the strings being passed are being used as googlemaps directions start and end points!


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    It's not the commas - you're passing in a string that's not in quotes.
    initialize(<%=str1%>,<%=str2%>)
    
    turns into
    initialize(hello, world, stupid, commas)
    

    You need it to be:
    initialize('hello, world', 'stupid, commas');
    

    So you need to do this:
    initialize('<%=str1%>','<%=str2%>');
    


  • Registered Users Posts: 46 Paddy GT


    eoin wrote: »
    It's not the commas - you're passing in a string that's not in quotes.
    initialize(<%=str1%>,<%=str2%>)
    
    turns into
    initialize(hello, world, stupid, commas)
    
    You need it to be:
    initialize('hello, world', 'stupid, commas');
    
    So you need to do this:
    initialize('<%=str1%>','<%=str2%>');
    


    Cheers Eoin ... knew it would be something ridiculously simple!! :o


Advertisement