Re: Javascript escape & JScript GlobalObject.escape

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: bruce barker (nospam_brubar_at_safeco.com)
Date: 03/15/04


Date: Mon, 15 Mar 2004 10:48:08 -0800

escape is a url escape and single quotes are legal in urls, so no escaping
required. if your are outputing a javascript literal, you need to do
javascript quoting (no builtin function for this). but easy to write.

  public static string JscriptQuote(string s)
  {
   s = s.Replace("'", "\\'");
   s = s.Replace("\n", "\\n");
   s = s.Replace("\r", "");
   return "'" + s + "'";
  }

-- bruce (sqlwork.com)

"Hugo Wetterberg" <hugo@wetterberg.com> wrote in message
news:1ssb50pcb9j9bc56ut6qqgqijjeq3is3oo@4ax.com...
> I've got a problem with the escape function in JScript.
> It simply doesn't escape the single quotation mark '
>
> This becomes a problem when I have to construct javascript calls.
> I'm trying to pass the url
> ref_handler.aspx?id=someguid&name=O'Brien
> as a parameter to a javascript function.
>
> What should have been:
> javascript:PopUp('ref_handler.aspx%3fid%3dsomeguid%26name%3dO%27Brien')
> becomes
> javascript:PopUp('ref_handler.aspx%3fid%3dsomeguid%26name%3dO'Brien')
>
> And this, of course, works just about as well as my latest perpetual
> motion machine.
>
> Comments?
>
> Cheers Hugo



Relevant Pages

  • Re: Getting Starting in JavaScript et al
    ... To date I have had three epiphanies where I have suddenly "got" javascript and realized how utterly diffident my previous understanding of the subject had been. ... Identifying and understanding the types of values that javascript uses; that there are 5 primitive data types (boolean, number, string, null and the undefined type). ... tab would then contain the tab character, ... Javascript has escape sequences that start with a backslash, but there are a number of types. ...
    (comp.lang.javascript)
  • Re: Macintosh dont see javascript?
    ... You don't need to escape the single quotes that delimit the literal strings in the Javascript because you used double quotes to delimit the whole attribute. ...
    (alt.html)
  • Re: block writing
    ... How can something like that suddenly appear in the javascript source? ... If you are generating the script, you can also automatically escape the ...
    (comp.lang.javascript)
  • Re: problem in reading server side string from client
    ... "Karl Seguin" wrote: ... javascript also needs slashes escapes. ... > or to escape it via: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: alert(escape("!"));alert(unescape("%u0021"))
    ... off-topic) but about the escape() function in JavaScript. ... In fact, the issue has nothing to do with UTF-8, the transfer encoding. ... Well, initially the % encoding (which is used in many contexts outside JavaScript, too) was defined for code points in the 8-bit range (0 to FF in hex) only, using %xx notation. ...
    (comp.lang.javascript)