Brian David Berman
Brian David Berman

Reputation: 7684

Prevent jquery from being referenced more than once?

Is there something I can wrap around the body of my jquery include file to prevent it from being included twice? My problem is that my application has multiple includes and I want to prevent jquery from being referenced in multiple includes and breaking.

My thoughts would be to open my jquery file (jquery-1.4.2.js) and put some sort of logic around the entire block of jquery library code.

Something like:

if (jQueryIsAlreadyIncluded()) { //jquery library code {(function(A,w){function ma(){if(!c.......

}

Upvotes: 2

Views: 916

Answers (3)

Russ Cam
Russ Cam

Reputation: 125498

perhaps

if (typeof jQuery === undefined) {

    // jQuery code

}

Upvotes: 1

Marco Johannesen
Marco Johannesen

Reputation: 13134

Hmmmmm Boilerplate does this when using the googleapi jquery, so you could use it for local files too

    <script>window.jQuery || document.write('<script src="yourpathtojquery"><\/script>')</script>

This is only for jquery itself though, otherwise you could set some variables and then in each library check if the variable is set, if not include, else exclude.....

if(!($Mylib))
{
    //Your javascript here....

    Mylib = true;
}

Upvotes: 2

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this

if(typeof jQuery == 'undefined'){
   //jquery library code {(function(A,w){function ma(){if(!c.......
}

Upvotes: 4

Related Questions