17

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

1
  • 2
    The clue is in the "not allowed to change" bit, what reason could there be for that restriction? Commented Sep 3, 2009 at 10:01

6 Answers 6

47

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

let foo = String.raw`<a href="javascript(foo('a','b'))">test</a>`
Sign up to request clarification or add additional context in comments.

2 Comments

Only without the @ it works in my Node v10.15.3 and Chrome 74.0.3729.131 (Official Build) (64-bit) on Windows 10.
So useful when dealing with latex equations with MathJax
4

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>';

Comments

2

Either escape the quotes within JavaScript:

var foo = "<a href=\"javascript(foo('a','b'))\">test</a>";
var foo = "<a href=\x22javascript(foo('a','b'))\x22>test</a>";
var foo = '<a href="javascript(foo(\'a\',\'b\'))">test</a>';
var foo = '<a href="javascript(foo(\x27a\x27,\x27b\x27))">test</a>';

Or escape the quotes within HTML:

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

Comments

2

There is no way to have characters in the string get escaped without escaping them in the string, such as using @-quoted that C# has. for example

string myString = @""Good Morning", said Dave's mother";

You need to escape the characters in the string in JavaScript using \ character.

Comments

1

This forum topic seems to have an 'interesting' alternative, at least. It uses multiline comments inside an anonymous function to enable strings containing both " and ', as well as multiline strings.

Edit: According to bucabay (in the comments below), this method no longer works, at least in Firefox 3.5.

3 Comments

that is a pretty neat solution. it has no dependencies outside the JS language. embedding it in HTML or XML is externally dependent.
I just tested this solution in Firefox3.5 and it doesn't work any more. Comments in function bodies are ignored by the Function.prototype.toString() method.
That's a shame. I was at work when I posted my answer, and so couldn't test it myself. Now I'm back home, I don't need to anymore.
0

No, there is no way. As the part that you are not allowed to change contains both quotation marks and apostrophes, there is no way to represent it as a string literal in Javascript without changing it.

Perhaps you can put it as a CData literal in an XML island in the page, and let the Javascript read it from there...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.