tags:

views:

309

answers:

4

I'm wanting to capture my search terms and pass them to a JavaScript variable, but I don't know how to handle quotes that might come through.

Here's what I have currently:

var searchTerms = "<!--#echo var="terms"-->";
var pattern = / /g;
newSearchTerms = searchTerms.replace(/[^a-zA-Z 0-9]+/g,'');
var searchStr=newSearchTerms.replace(pattern,"_");

I'm concerned that should "terms" contain double quotes (or an apostrophy if I use single quotes in the JS) then my function will fail.

How do I escape the string before it gets into script?

Thanks,
Steve


Edit/answer: I ended up doing this by moving this to an external script that captured and parsed the querystring rather than echoing it in the HTML.

+3  A: 

If terms contains quotation marks, by the time you have done var searchTerms = "<!--#echo var="terms"-->"; it is already too late to replace any quotation marks, your JavaScript will be invalid. For example, if terms contains These are the "terms" your JavaScript would appear as follows (and produce a syntax error in the browser):

var searchTerms = "These are the "terms"";

If you are sure terms only contains double-quotes, you could do:

var searchTerms = '<!--#echo var="terms"-->';

If it could contain both single-quotes and double-quotes, you need to sanitize the output on the server using a server-side technology more sophisticated than <!--#echo var="..."-->.

Grant Wagner
Alternatively, I think you can set a variable to '"' in JavaScript. Example: var DoubleQuotes = '"' var searchTerms = "<!--#echo var=" + DoubleQuotes + "terms" + DoubleQuotes + "-->";This is useful when Obfuscating.
Dalin Seivewright
I don't think the server will process that statement as shown. I could be mistaken though.
Grant Wagner
@Dalin - I can't imagine the server being able to deal with that, but I'm interested in the idea that you can set DoubleQuotes. Could I read that to mean that I could do: DoubleQuotes = '"'; var searchTerms = DoubleQuotes + <!--#echo var="terms"--> + DoubleQuotes;
Steve Perks
DQ = '"'; var searchTerms = DQ + <!--#echo var="terms"--> + DQ; won't work. Given the same string above, you'd end up with var searchTerms = '"' + These are the "terms" + '"'; The result of the #echo isn't valid JavaScript, you'd still get a client-side syntax error.
Grant Wagner
+1  A: 

i would add a javascript to the onchange event for the search textbox. capture the keystroke and ignore the quotes and any other special characters that might be entered. if the input is coming from the server side, then sanitize it before sending it to your script.

Victor
+2  A: 

From your code it looks like you're using Apache SSI includes. The echo SSI has an attribute called encoding which wil let you specify url-style encoding. You can encode quotes this way and simply unencode in Javascript with unescape()

Try this:

var terms = "<!--#echo encoding="url" var="terms"-->";
terms = unescape(terms)
Triptych
Sorry Triptych but that's not working on my set up of SSI, though I think I'm going to have to go down that path (unless I can get some more mileage out of Dalin's comment)
Steve Perks
note that order is important; the "encoding" attribute MUST come before the "var" attribute.
Triptych
yeah - I copied it verbatim. Our server's got minimal stuff turned on.
Steve Perks
A: 

Both the idea of encoding the output and capturing and escaping the keystrokes are interesting solutions. Encoding the SSI output isn't working for me at the moment, though I'm setting that option in motion, but escaping the keystrokes makes me nervous as I'm not sure how well our search engine will deal with it.

Thanks for the two pointers towards a possible solution.

Steve Perks