Script tag in JavaScript string?

Up vote 3 down vote favorite 2 share g+ share fb share tw.

I am encountering an issue where having a ending script tag inside a quoted string in JavaScript, and it is killing the script. I assume this is not expected behaviour. An example of this can be seen here: jsbin.com/oqepe/edit My test case browser for the interested: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.4) Gecko/20091028 Ubuntu/9.10 (karmic) Firefox/3.5.4.

Javascript link|improve this question edited Dec 1 '09 at 10:06Peter Mortensen4,35342051 asked Nov 2 '09 at 6:41re5et810212 82% accept rate.

The browser HTML parser will see the within the string and it will interpret it as the end of the script element. Look at the syntax coloring of this example: bar.....'; Note that the word bar is being treated as text content outside of the script element... A commonly used technique is to use the concatenation operator: var test = '...... ......

This does work, but I am surprised that I have to do it. Part of the problem is that I am scraping a page and storing the results in a JS variable. I have no real expectations of what Is coming back.

– re5et Nov 2 '09 at 6:51 How are you storing it in a variable? Are you scraping server-side then generating var x = ;? If so, don't forget to JSON-encode it.

– orip Dec 1 '09 at 10:10 Escape the /, don't split the string up into parts. IIRC it is still an error in HTML 4.x. It is certainly more fiddly to type, messier to read, more characters to deal with, and less efficient (since string concatenation isn't the cheapest of JS operations) – Quentin Dec 1 '09 at 10:20 great I never knew it.

:) – Rakesh Juyal Dec 1 '09 at 11:37.

You need to escape it, else it will be a part of the HTML. Var test = 'what the hell... \ \why?!?!?!

2 this is incorrect. This is a variable that is storing text. It is not inserted into the HTML.

Using a tag other than script will not have the same results. – re5et Nov 2 '09 at 6:47 It's the way XML is being rendered. You can also wrap the script with .

It won't happen with other tags because the way the XML parser work (notably it treats script as text, and not as code). – LiraNuna Nov 2 '09 at 6:50 @LiraNuna Umm. No.

An XML parser will treat as "End of script" and as a well-formness error. An HTML parser will treat as "end of script" and then if it is as "Error with handling undefined by the specification". Only a tag soup parser (and possibly an HTML5 parser, I haven't read the draft closely enough to be sure) will treat as part of the script.

– Quentin Dec 1 '09 at 10:19 Oh, and if you wrap with CDATA markers then that won't fix it for tag soup parsers. – Quentin Dec 1 '09 at 10:21.

Another way to go arround this problem, and the most standart way would be doing something like this: bar.....'; .

This does not work. – Tom Ritter Jun 11 '10 at 21:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions