CommonSpot ADF’s renderScriptOnce method

A problem common across many CMS platforms is managing the loading of third-party JavaScripts, such as JWPlayer. You could make them available for any page in your website by including them in a global header or footer, but that approach is lazy and wastes bandwidth. A better approach is to include third-party JavaScripts on only the pages where they will be used.

For example, pages with embedded videos get the JWPlayer linking, but the pages lacking videos do not. This is fairly straightforward– just include the third-party JavaScript in the output of the plugin, render handler, etc. with which it is associated (i.e. your video element/plugin links to the JWPlayer script).

But what if a page has multiple instances of that video element/plugin?

That’s right– you get multiple invocations of that third-party JavaScript, which can be a bad thing depending upon which JavaScript tool you’re using.  JWPlayer is written in such a way that multiple invocations/instantiations won’t break it (as a matter of fact if you’re using the JW Platform, you don’t have any other option), but some other JavaScript tools are not that flexible and forgiving. Even if a third-party JavaScript can safely be invoked and instantiated multiple times, that is still extra processing time on the client side. When you’re trying to be fast and lean, doing the same thing over and over again is counter-productive.

What we really need here is a way to load third-party JavaScripts once and only once. Fortunately, if you are working with PaperThin’s CommonSpot CMS, their Application Development Framework has a ScriptService component with a sweet little method called “renderScriptOnce.”

Here’s an example using the renderScriptOnce method to load JWPlayer:

That’s a nice little gem tucked away inside of CommonSpot’s ADF, and I hope it will save you both time and aggravation.