DDiVita
DDiVita

Reputation: 4265

Web custom control and checking for existing javascript references?

I am creating a custom control and had a thought before I began. My control relies on the jQuery library to work. What if the user already has a version of the library already on their page. Will this effect anything? If my version is newer / older and my control will only work with that version of the libaray, what should I do? Thanks

Upvotes: 2

Views: 234

Answers (1)

Martin Buberl
Martin Buberl

Reputation: 47144

Well, you could check in your scripts your control is rendering if the jQuery function is defined.

if (typeof jQuery == 'undefined') {  
  someLoadMethodJustForExample("jquery");
}

If not, it isn't loaded. And you should load it.

If there's already another version loaded jQuery.fn.jquery; will return you a version string like "1.4.4". So you could display an error message and tell the person to reference another jQuery version which is compatible with your control or just let your control do the work.

Google does have a dynamic loading function for jQuery (and a lot of other frameworks) you could use.

google.load("jquery", "1.4.4");

Hope that helps.

Upvotes: 3

Related Questions