views:

59

answers:

2

I'm creating a website that could potentially contain many widgets. These widgets are rendered using external javascript code in external js files. Some of these widgets require the same external javascript files but i obviously dont want the browser including (ie: ) a javascript file whenever a widget requires one; instead it will only include a javascript file when it has not already been retrieved or is in the process of retrieving one.

Is there a javascript manager that can fulfill the requirements above? Extra points will be given to the solution with jQuery in mind :).

A: 

I use Google's AJAX API to load jQuery and various other libraries I use.

It cuts down on bandwidth since the user won't get the resource from my server, and also the odds of them having the library are higher.

Here's a link.

Joseph
I don't think he's talking about libraries, he's talking about his own custom code. But I agree that unless he has a CDN of his own, he should be using Google's hosted libraries.
Justin Johnson
A: 

Dojo's dojo.require and dojo.provide does exactly what you are asking for. Example:

dojo.require("com.project.widgets.someWidget");

Which would load com/project/widgets/someWidget.js. Inside of that file, you would have

dojo.provide("com.project.widgets.someWidget");

Which registers the source as loaded.

It basically does a synchronous AJAX request, eval's the file, and marks the path as "fetched" so that it doesn't make the same request twice"

Justin Johnson