How to check if jQuery is already loaded in Magento
If you are building some custom extension for Magento and your extension includes jQuery, but you don’t want to load it if its already loaded by some other Magento extension or theme, then you can do so using your layout XML file as shown below. Here we are checking for the global window.jQuery variable. If that is undefined jQuery is not loaded yet. Add the following to your modules layout XML.
<default> <reference name="head"> <block type="core/text" name="jquery_load"> <action method="setText"><text><![CDATA[if (!!!window.jQuery){ document.write('<script type="text/javascript" src="url/to/jquery/file.js"></script>');}]]></text></action> </block> </reference></default>
This will be included in the head after all other javascripts are loaded and you can be sure jQuery wasn’t included if its already there.
Comments
Post a Comment