Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign javascript string without escaping

Tags:

javascript

Hi I have a problem with a javascript string

var foo = \"<a href="javascript(foo('a','b'))">test</a>\"

This sentence gives me an error

I could have escaped inner " but I am not allowed to change <a href="javascript(foo('a','b'))">test</a> this part

Is there any way to handle this condition?

Thanks, Sourabh

like image 425
Sourabh Avatar asked Sep 03 '09 09:09

Sourabh


2 Answers

Several years later, this is now possible with String.raw()

let foo = String.raw`<a href="javascript(foo('a','b'))">test</a>`
like image 85
bviale Avatar answered Sep 25 '22 09:09

bviale


No, you need to escape the string somehow.

var foo = "<a href=\"javascript(foo('a','b'))\">test</a>";

or

var foo = '<a href="javascript(foo(\'a\',\'b\'))">test</a>';
like image 35
Greg Avatar answered Sep 25 '22 09:09

Greg