0

Javascript function:

var Id ; //global variables
var Name; //global variables
function getServiceId(id,name){
    Id=id;
    Name = name;
    alert(Id);
}

And this my jsp code

<core:forEach var="service" items="${listServiceBO}">
                                <tr>
                                    <td><a href="Javascript:void(0);">
                                            ${service.name}
                                    </a></td>
                                    <td>${service.multiplicity}</td>
                                    <td>${service.scheduleType}</td>

                                    <td>
                                        <button type="button" id="serviceDeleteButton(${service.id})" name="serviceDeleteButton" 
                                            onclick="getServiceId(${service.id},'${service.name}');" title="Delete"
                                            class="btn btn-link btn-inline" data-toggle="modal"
                                            data-target="#deleteServiceModal"> 
                                            <span class="glyphicon glyphicon-remove"></span>
                                            <span class="sr-only">Delete</span>
                                        </button>
                                    </td>
                                </tr>
                            </core:forEach>

if my service.name contains any special character....my values are not getting set in Javascript. But if I do not have any special character in that name then it is working fine.

Because of special character I am not able to set any of the two values. Any solution ???

Crawling
  • 87
  • 11

2 Answers2

0

Can you try this:

onclick="getServiceId(${service.id},&quot;${service.name}&quot;);"

Or, the best thing to do would be escaping all values in Java:

Base64.encode(str);

and in Javascript doing a decode:

decodeURIComponent(str);
ArinCool
  • 1,720
  • 1
  • 13
  • 24
0

edit:

Try just escaping it:

var Id ; //global variables
var Name; //global variables
function getServiceId(id,name){
    Id=id;
    Name = escapeRegExp(name);
    alert(Id);
}

function escapeRegExp(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

jsp not strong suit,but maybe change this:

onclick="toggle('getServiceId(${service.id},'${service.name}')');" 

to:

onclick="toggle('getServiceId(${service.id}, \"${service.name}\")')"
Community
  • 1
  • 1
Todd
  • 5,314
  • 3
  • 28
  • 45