View Full Version : How to handle string that may contain anything?

Mar 22nd, 2003, 11:46 PM
I have a variable set like so:
var edtxt = """ & addrsde(5) & """;

The string is just text from a form. Problem is, it may contain characters that javascript will find unacceptable, such as semi colons, brackets, etc. Is here any way to deal with this so that I can get into edtxt the actual text without getting a javascript error?


Mar 22nd, 2003, 11:57 PM
This is not an lexically acceptable JS expression: var edtxt = """ & addrsde(5) & """;

This is: var edtxt = '"" & addrsde(5) & ""'; - since it replaces the double-quote delimiters with singles, avoiding any confusion with the double-quote characters. If you're assigning this value from a form field, there aren't any delimiters involved, it's just a (primitive) string value going into a variable. JS finds almost any string (character) value 'acceptable', as it's just data. Collisions can occur during processing, but you'd have to specify the process that's being corrupted...

Mar 23rd, 2003, 12:42 AM
Well, that expression i showed was within an asp Response.Write statement. So it does work without any javascript errors. Here is what the full statement looks like:
Response.Write("var edtxt = """ & addrsde(5) & """;")

To be precise, I am trying to populate an iframe from a database. It works mostly, but chokes on some strings. When it chokes, the iframe does not get filled in and I get a 'done' in the ie status with a little yellow flag next to it. Is there a way to find out what errors the flag is referring to? The whole text does fill in for that variable when I peek at the html generated, so maybe it is picking the text up ok.


Mar 24th, 2003, 02:26 AM

Mar 24th, 2003, 06:57 PM
Thanks Glen, that is useful. I still am getting an 'unterminated string literal' error. There are tags, which I have escaped e.g: <p> becomes \<p\> .. The text that is filled into the variable is many lines. Could the fact thiat the variable is wrapped over many lines be the problem? The netscape debugger points to the problem in this area, many lines into the variable (right beyond the first 'p' tag.
in the Internet industry.\<\/P\>
\<P\>There are


Mar 24th, 2003, 07:25 PM
You can't break the line inside a string literal
var myString = "bl
ah";will error, just like
var html = "<p>
this is a paragraph
</p>";will error. You have to use
var html = "<p>this is a paragraph</p>";or
var html ="<p>";
html += "this is a paragraph";
html += "</p>";Now the error message 'unterminated string literal' should make sense ;)

Mar 24th, 2003, 08:44 PM
Thanks, Beetle... Searching around I had come to that realization. But if I am pulling an article out of a database into the variable, not sure how I build that into the variable so that I dont break lines

Mar 24th, 2003, 08:59 PM
Could mismatched quote marks embedded in the string cause a problem like this? Or maybe characters treated as delimiters or terminator signals by the mechanism retrieving the string. Or the string is too long. Or has an object embedded in it. Truncation? Or maybe the data has an embedded string in it that IS unterminated. Or...

I'm just guessing and trying to be helpful. :)

Mar 24th, 2003, 09:44 PM

Yes, I wonder, too. I had one large article that went into the javscript variable with no problem and one that did not. So, although it seems like exceeding the string limit of 255 characters or whatever it is or extending across lines ought to be the problem, it may not be so.


Mar 25th, 2003, 09:24 AM
do you replace all occurrences of VBCr and VBLf to \r and \n?
can you post your vbscript function that "escapes JS" just like the one in the link I provided?

Mar 25th, 2003, 03:37 PM
Thanks, Glen, that did the trick!! Here is what I used:

str = Replace(str,"/","\/")
str = Replace(str, vbCr, " ")
str = Replace(str, vbLf, " ")