Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  39] [ 7]  / answers: 1 / hits: 45318  / 15 Years ago, mon, november 2, 2009, 12:00:00

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: http://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.


More From » javascript

 Answers
8

What happens?



The browser HTML parser will see the </script> within the string and it will interpret it as the end of the script element.



Look at the syntax coloring of this example:



<script>
var test = 'foo... </script> bar.....';
</script>


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 = '...... </scr'+'ipt>......';

[#98407] Wednesday, October 28, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hasanjustusf

Total Points: 76
Total Questions: 116
Total Answers: 100

Location: Virgin Islands (U.S.)
Member since Tue, Jul 7, 2020
4 Years ago
hasanjustusf questions
;