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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Passing Strings with commas to a javascript method

  • 11-08-2010 7: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,263 ✭✭✭✭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