Passing a string with imbedded "'" into Javascript function

Posted on 2004-11-16
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 (').

Question by:PSernz
    LVL 33

    Accepted Solution

    try this

    <script language="JavaScript">
    function perdinfo(a, str)

    dim perddesc

    perddesc = "Hello World's"
    'perddesc = "Hello World"

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

    LVL 3

    Expert Comment

    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:
    <body onLoad="perdinfo(this,uniqueName);">

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

    Expert Comment

    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.


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

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

    Expert Comment


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    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…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now