Passing a string with imbedded "'" into Javascript function

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
PSernzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hongjunCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wzd3Commented:
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
wzd3Commented:
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
hongjunCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.