Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-02-03 14:07:24

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Avoid loading jquery ui multiple times

Hi,
I’m stuck with this issue:
I want to load the same jquery ui script from inside a couple of plugins. The problem is I’d prefer to avoid loading it multiple times in the same page when both plugins are active.
Is there an easy way to load the script only if it is not already loaded?
Any help appeciated, thanks.

Offline

#2 2011-02-03 15:03:01

maniqui
Moderator
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Avoid loading jquery ui multiple times

Hi redbot.
Maybe you could set some kind of flag (JQ_UI_LOADED) on all your plugins, and test for its value in every plugin. If true, then don’t load it again.
Or, maybe you could adapt this trick from html5boilerplate:

<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script>

La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#3 2011-02-03 15:50:32

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Avoid loading jquery ui multiple times

Thank you Juliàn!
Unfortunately I think your first suggestions won’t work in my case.
It’s a bit tricky but I’ll try to explain.
The fact is I can easily check from inside the <script></script> part of my plugin if the jquery ui script is already loaded using:

    
if (jQuery.ui) { 
    //do things 
}

The issue is —if the condition isn’t true— I can’t then load the script from inside the <script> tag.
In other words I can’t write:

    
<script language="javascript" type="text/javascript">
   //....
    if (!jQuery.ui) { 
        <script language="javascript" src="jquery-ui-1.8.9.custom.min.js"></script> 
    }
</script> 

Your second suggestion seems interesting though. I’ll have a deeper look later and let you know if it works.
Thanks again

Last edited by redbot (2011-02-04 00:25:39)

Offline

#4 2011-02-03 15:56:08

maniqui
Moderator
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Avoid loading jquery ui multiple times

Hi redbot.
You are welcome.
Regarding the first suggestion, I was thinking on a PHP solution, not a JS one. That could make a difference in the approach to tackle this.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#5 2011-02-03 16:28:58

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Avoid loading jquery ui multiple times

maniqui wrote:

Regarding the first suggestion, I was thinking on a PHP solution, not a JS one. That could make a difference in the approach to tackle this.

Er… this is where my original question originates ;-)
The only “php way” which I can conceive is to check if the script file does exist in a specific directory in the site. But this is only useful regarding my plugins since it won’t keep into account those who are loading the script from the internet or from another directory defined by some third party plugin.

Anyway probably I’m trying to cover too many situations – your suggestions will work in the majority of cases. Thanx :-)

Offline

#6 2011-02-03 18:14:41

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,461
Website

Re: Avoid loading jquery ui multiple times

redbot, are there not ways of dynamically loading javascript scripts with jquery? maybe getScript? Example

I forget now exactly where I saw it but while I was looking for a lazyload-like plugin that dynamically loads additional articles I remember getting lots of matches for plugins that loaded js libraries as needed.

Last edited by jakob (2011-02-03 18:15:28)


TXP Builders – finely-crafted code, design and txp

Offline

#7 2011-02-03 18:15:58

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,251
Website

Re: Avoid loading jquery ui multiple times

You could load jQuery UI from with javascript using Google’s Javascript API


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#8 2011-02-04 00:12:06

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Avoid loading jquery ui multiple times

Thanks Jacob! getScript() is just what I was looking for. I don’t know how I could have missed it.
Thank you Juliàn and Matt also for your suggestions, very appreciated.

Offline

#9 2011-02-10 19:52:28

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,251
Website

Re: Avoid loading jquery ui multiple times

Turns out I have a plugin that uses jquery-ui and goes haywire if your plugin using jquery-ui is active as well. How did you solve this?


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#10 2011-02-10 20:02:19

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Avoid loading jquery ui multiple times

MattD wrote:

Turns out I have a plugin that uses jquery-ui and goes haywire if your plugin using jquery-ui is active as well. How did you solve this?

Hi Matt,
at the end, after a lot of testing, I changed idea and found that using getScript() in my case was not worthwhile since it slowed things a lot (and the script was not not always cached even using ajax() instead of getScript(), but that’s another story…).
Off the top of my head I think maybe is because we are using two different customized jquery ui scripts? I’ll have a look in the next days.
Keep an eye on the bot_image_upload o smd_admin_datepicker thread.

Offline

Board footer

Powered by FluxBB