Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why escaping / in javascript '<\/script>'?

Tags:

javascript

I have seen everyone doing it tho i dont' get why.

document.write('<script src="src"><\/script>');

I mean using the single ' you shouldn't need to esacape chars?

like image 600
dynamic Avatar asked Mar 29 '11 17:03

dynamic


1 Answers

  1. Single and double quoted strings have the same escaping rules.
  2. It prevents </script> from being parsed as an closing tag.

"</script> and "<\/script>" are the same to JavaScript, but only the first one is interpreted by the HTML parser to contain a HTML closing tag.

like image 152
Matthew Flaschen Avatar answered Sep 25 '22 13:09

Matthew Flaschen