SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to escapa or include a single quote in jscript function call

    Hi,
    How can we include a single quote in a string when being passed as a parameter in the jscript api. I tried escaping it but no luck.

    Below is the code snippet
    ======================
    <BODY onLoad="javascript:calledOnLoad('<%=params%>','<%=context%>')">

    In the above call, "params" would contain value as '#cname=swgopal's comp,#age=40,#pin=100 '

    Notice a single quote " ' " in #cname=swgopal's. Because of this single quote, the calledOnLoad function parameter is getting ended and the error says missing ")"

    Could some one suggest a possible solution for this.

    Thanks

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Prefix the apostrophe with a backslash character: \'.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) Don't ever use "javascript: <anything>" in your html.

    2) It's better design not to put onload event handlers in the body tag. Instead write:
    Code:
    <script>
    
    function calledOnLoad(x, y)
    {
        ///
    }
    
    window.onload = function(){ calledOnLoad("value1", "value2"); };
    
    </script>
    </head>
    That would also solve your quote problems because you could write:

    window.onload = function(){ calledOnLoad("<&#37;=params%>", "<%=content%>"); };

    It's even better practice to link to .js files in the <head> section:

    <script type="text/javascript" src="myFile.js"><script>

    Then you would write to the text file myFile.js instead.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •