Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JavaScript libraries - best practice?
#1
When developing a theme or a plugin that requires a JavaScript library, isn't there a risk of different libraries colliding? Could/should Get Simple have something in its API for helping with this? I wonder if, in the future, JavaScript libraries should be provided as plugins, and plugins should have ways of stating that they require another plugin to be enabled before they themselves can be enabled? This could guard against problems like Connie's 4th point here.

Also, related to this, different people have different views on where JavaScript should be placed in the HTML. I've just started a theme which is based on HTML5 Boilerplate, where JS libraries and scripts are placed mostly at the end of the HTML. However, the I18N Gallery plugin places its link to jQuery and all its scripts in the <head> - presumably because this is the only place into which plugin developers can reliably inject their code - through the get_header() template tag.

So, on that second issue, would it be good to introduce a new template tag/function - get_scripts() - to allow themers to decide where to include this stuff?
Reply
#2
There is a system in place for this called script/style queueing (as detailed on the wiki).

As an example, jQuery is a preloaded variable in this, so all I have to do if my theme is dependable on jQuery is include the following line of PHP:
Code:
queue_script('jquery', GSFRONT);

The only problem with this system is that where the script is included (header or footer) is set when you register it and not when you include it.

If everyone makes use of this system, in theory, we will never include the same libraries multiple times. For example, if my plugin makes use of MooTools I will register it:

Code:
register_script('mootools', '//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js', '1.4.5', FALSE);

And use the queue to load it:

Code:
queue_script('mootools', GSFRONT);

If your plugin were to do the same 2 steps it will overwrite my registration of the script (there is no check for overwriting in the core, which I find a little disconcerting) but because we will both use the same keyword to queue the script will only be included once in the code.

If you have any ideas on how to make this system better please let us know.
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply




Users browsing this thread: 1 Guest(s)