0

I've noticed some sites use this method as a backup source if Google's hosted jquery fails to load. I copied it too, and am now curious about the syntax:

window.jQuery || document.write('<script src="/js/jquery.min.js"><\/script>');

As I understand it, if jQuery is false or undefined, then write out a script to load jquery locally.

What I'm unsure of is, what is the point of the \ in the closing script tag? what is there to escape?

Thanks!

d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • 2
    possible duplicate of [Why do I need to escape the '/' character in JavaScript?](http://stackoverflow.com/questions/2170609/why-do-i-need-to-escape-the-character-in-javascript) and [What's <\/script> for?](http://stackoverflow.com/questions/9188321/whats-script-for). – Felix Kling Feb 08 '13 at 15:23

1 Answers1

1

You are escaping the whole </script> tag itself. This is because, many of the older browsers think that the script gets ended here:

window.jQuery || document.write('<script src="/js/jquery.min.js"></script>');
-----------------------------------------------------------------^

That point becomes the end of the original <script> tag!

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252