Javascript question (how to pass strings with white space?)

Although this is a javascript question, it's related to VE because I'm writing a webservice function which writes javascript on the server-side.

I have a simple question. I want to pass a string value (in my case a tag), to a javascript function:

So I have someting like the following:

<pre>
tagCloud.Append("<a href=# onclick=SetTag(&quot;" + tag + "&quot;) >" + tag + "</a> ");
</pre>

eventually this is rendered to screen where I have the typical tagcloud that allows a user to click tags and filter stuff.

The problem is that when the value "tag" contains whitespace the javascript breaks. I thought by putting &quot; I would solve this... but it's obviously not working. What's the solution I figure this should be easy to fix.

thanks...


Answer this question

Javascript question (how to pass strings with white space?)

  • Toni Greco

    By the way that code line is C# - in this case it matters a bit as the syntax is peculiar and probably not the same in VB, if that's what you're using server-side.

    In C#, strings are literally as written except whenever a backslach character is encountered; whenever this happens a single character in the string is described by the backslash plus the following character in the source code. Javascript uses the same system. In C# there's an additional facility to tell the compiler to interpret a string, including backslashes, literally as written - precede the string with the '@' character. Thus, replacing "\n" with @"\n" means to replace character value 10 (newline) with a sequence of two characters, a backslash and an 'n'. The generated javascript would thus be

    var s = "toto \n tata";

    instead of the code being on two lines. I hope you understand why the code is as it is so you can translate it to VB if required. If not, say so and I'll try to translate it to VB for you tomorrow (I'm in Paris and it's 11 in the evening now, I'm signing off).


  • logtorahul

    hmm... javascript didn't like it for some reason. thanks for the reply though.


  • William Bartholomew

    Hi,

    the javascript doesn't "break" as you say if the string tag contains whitespace in general. Whitespace in the form of spaces certainly present no problems. Tabs might not be truthfully represented (because the js string won't contain any tab characters, just a string of spaces that depends on how the tab character is printed by the HtmlTextWriter rendering the response), but they won't cause script errors.

    Newlines and carriage returns are another matter. This makes perfect sense, and would be the same if you were generating C# code:

    string s = "toto

    tata";

    is illegal. The reason it is so is because it makes a mockery of indentation and organizing code into lines; everything is part of the string here!

    Those are not the only problems you have though. If the string contains double quotes you'll end up with

    var s = "toto "tata" titi"; // tried to use s: toto "tata" titi

    Also, backslashes in the string, say, c:\inetpub\wwwroot, would be interpreted as escape characters, causing at best misinterpretation of the string and at worst crashes.

    So you need to replace @"\" with @"\\" (equivalently, "\\" with "\\\\\"), and "\"" with "\\\"", "\n" with @"\n" and "\r" with @"\r" and ideally "\t" with @"\t" as well. Since you need to replace newline with a sequence of backslash plus 'n', and similarly for other nonprintables, it is imperative that you replace characters in the correct order - otherwise the escape characters will become literal backslashes!

    This all seems a little confusing, but it really isn't difficult and it boils down to a single line of code:

    tag = tag.Replace(@"\", @"\\").Replace("\"", "\\\").Replace("\n", @"\n").Replace("\r", @"\r").Replace("\t", @"\t");

    This takes care of backslash, double quotes, newlines, carriage returns and tabs. I may well have overlooked something, but this gets you well on the way and you can easily add any other exceptions as you discover them. It'd be nice of you to share them with the rest of us on this forum though!

    HTH, Dag


  • Magannahan Skjellifetti

    Would doing a string.replace() work for you Find the whitespace and repalce it with the ASCII code, perhaps

    tag = tag.replace(" ", "&#32;");


  • Javascript question (how to pass strings with white space?)