1

I used to be checking for the presence of globally available JS libraries by checking the content of the window object. Now I am thrown back by the fact that if I check for say jQuery and have an element like this: <div id="jQuery"> on the page, it will be available under window.jQuery.

How is this a good thing?

Example: http://jsfiddle.net/7xVeJ/2/

Am on Chrome 16.0.912.75 on Linux

Radek
  • 3,913
  • 3
  • 42
  • 37

1 Answers1

3

Basically, that's because named elements were exposed as properties of the document object in early versions of the DOM interface. Then some browsers decided to also expose these properties on the window object. See bobince's answer here for the full story.

To work around this situation, you can test that window.jQuery is a function:

if (typeof window.jQuery === "function") {
    // jQuery should be available.
}
Community
  • 1
  • 1
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479