Home > DeveloperSection > Forums > Launching jQuery plugins depending on the user location
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    May-15-2013 8:02 AM

 JavaScript JavaScript 
Ratings:


 1 Reply(s)
 659  View(s)
Rate this:
Hi Guys!

My senior gave me a task to put all little javascript and jQuery plugin code in one file which will execute these plugins, depending if the page has certain .classes or #ids. I am thinking what is the best way to do that.

Will simple if -> else do in this case or should I go further and create something with .each?

I also think that with the growing amount of plugins and other client-side code, there will be significant performance issues. But so far we only have about 5-10 self-invoking functions.

Thanks in advance! 



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-15-2013 8:51 AM

Hi Ankita!

How about something like this? It depends on how many elements it's going to have to cycle through. You could be more specific with span's, or div's if need be.

var a_i = 0, b_i = 0;

$("#ids").each(function() {
    a_i++;
});

$(".classes").each(function() {
    b_i++;
});

if(a_i != 0) {
    $.getScript("js/specific.plugin.min.js");
    $.executePluginFunctionNameThingyHere();
}

if(b_i != 0) {
    $.getScript("js/another-specific.plugin.min.js");
    $.executeOtherPluginFunctionNameThingyHere();
}

Not tested - but it's the general idea.

Don't want to miss updates? Please click the below button!

Follow MindStick