In my custom JS code file I use various plugins, for instance, the Masonry plugin for JQuery. However, I don’t include the plugin on every page or at the master page file level (in some yes, in some no) but I do include my custom file on every page. Therefore, the code in my JS file will issue an error if the Masonry plugin is missing.
If someone could reply with a solution of how to:
- Check if certain JS plugin was referenced (included) and only run the code then,
- (optionally) include the file at runtime if it is missing.
I have it working with JQuery and JQuery UI because they both introduce a class into DOM structu (jquery and jquery.ui objects) but there are other plugin’s like lightbox scripts, the forementioned Masonry etc..
Rather than checking whether a particular JavaScript file has been ‘included’, I’d just check if the functionality that you need is available (i.e. if you need a function named
foo, just do something along the lines oftypeof foo == 'undefined'to see if it is there …).