Andy’s Scribbles

in JavaScript

I’ve updated the code I use to require scripts and styles on my web pages.

Check it out, or fork it at github: http://github.com/thetalecrafter/require

Usage:

main file:

1
2
3
4
require.setObjUrl('jQuery', function(name) {
return name === 'jQuery' ? 'http://code.jquery.com/jquery-1.5.2.min.js' :
'http://cdn-' + (name.length % 4) + '.example/plugins/' + name + '.js'; });
require('jQuery.myplugin', function(myplugin) { /* both have loaded when this executes */ });

plugin file:

1
2
3
require('jQuery', function(jQuery) {
jQuery.myplugin = ...
});

require css: Any requirement matching /\.css$/i will be treated as a css requirement.

1
require('myplugin.css', function() { /* You can count on styles being available here */ });

require image: Any requirement matching /.(?:gif|jpe?g|png)$/i will be treated as an image requirement.

1
require('myplugin_bg.png', function() { /* You can count on the image being available here */ });