{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rickymoorhouse.uk/blog/2010/2010-06-21-only-load-jquery-if-not-already-present",
  "path": "/blog/2010/2010-06-21-only-load-jquery-if-not-already-present",
  "publishedAt": "2010-06-21T14:01:59.000Z",
  "site": "at://did:plc:r53zv4vpzeihop3aliwyejlu/site.standard.publication/3mos5q3a7jf2w",
  "tags": [
    "Javascript"
  ],
  "textContent": "Very useful method of ensuring jQuery is loaded before running the code that needs it - from How to build a web widget (using jQuery) - Alex Marandon\n\n[js]\n/***** Load jQuery if not present ******/\nif (typeof jQuery === \"undefined\" || jQuery.fn.jquery !== '1.4.2') {\n    var script_tag = document.createElement('script');\n    script_tag.setAttribute(\"type\",\"text/javascript\");\n    script_tag.setAttribute(\"src\",\n      \"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\")\n    script_tag.onload = main; // Run main() once jQuery has loaded\n    script_tag.onreadystatechange = function () { // Same thing but for IE\n      if (this.readyState == 'complete' || this.readyState == 'loaded') main();\n    }\n    document.getElementsByTagName(\"head\")[0].appendChild(script_tag);\n} else {\n    main();\n}\n[/js]",
  "title": "Only load jQuery if not already present"
}