Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Passing Strings with commas to a javascript method

  • 11-08-2010 08:36PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 21,278 ✭✭✭✭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, Registered Users 2 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