?
Solved

Passing a string with imbedded "'" into Javascript function

Posted on 2004-11-16
4
Medium Priority
?
171 Views
Last Modified: 2010-07-27
How do eliminate problems if I want to pass a string that may have an apostrophy (') into a JavaScript functions.

The HTML is as follows :

<body onLoad="perdinfo(this,'<%=perddesc%>');">

The ASP string perddesc may have an imbedded (') and this causes the JavaScript to fail.
How is this overcome ??  Surely JavaScipt can take a string with imbedded (').

Peter
0
Comment
Question by:PSernz
  • 2
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 2000 total points
ID: 12601124
try this


<script language="JavaScript">
function perdinfo(a, str)
{
    alert(unescape(str));
}
</script>

<%
dim perddesc

perddesc = "Hello World's"
'perddesc = "Hello World"
%>

<body onLoad="perdinfo(this,'<%=escape(perddesc)%>');">

</body>
0
 
LVL 3

Expert Comment

by:wzd3
ID: 12601219
I suspect that that would have the same syntax problem.  The ' in perddesc will terminate the quote for the second argument, and then error. One way to solve it is to escape the quote in perddesc, but that may not be possible.

Another way is to break the code into two lines like:
<script>
uniqueName="<%=perddesc%>";
</script>
<body onLoad="perdinfo(this,uniqueName);">

My test example that works:
<script>
str="don't do that";
</script>
<form>
<input type=button name=test value=test onclick="alert(str)";>
</form>
0
 
LVL 3

Expert Comment

by:wzd3
ID: 12601282
Oh sorry: correction, I think your code will work hongjun, my bad.    I reread it and realized that you were calling escape in the asp processing.  Missed that the first time :-(  

I think you could even put the unescape into the body tag if you can't/don't want to change perdinfo.

Would

<body onLoad="perdinfo(this,unescape('<%=escape(perddesc)%>'));">

work as well?  I don't have a way to test asp code....
0
 
LVL 33

Expert Comment

by:hongjun
ID: 12601287
:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question