Avoiding multiple jQuery files?

Discussion forum for MOD Writers regarding MOD Development.
User avatar
Sajaki
Registered User
Posts: 1390
Joined: Mon Mar 02, 2009 1:41 pm
Location: Amsterdam
Contact:

Re: Avoiding multiple jQuery files?

Post by Sajaki »

What i do is include a file in overall_header that holds my required scripts, with "hooks" for plugins.
i also include jquery tools, and that is dependant on jquery being loaded before.

I think the "hook" system is already being discussed in area51 for templating.

Code: Select all

		
<open src="styles/prosilver/template/overall_header.html">
	<edit>
		<comment lang="en"><![CDATA[install bbDKP header - All in one file]]></comment>
		<comment lang="fr"><![CDATA[installation du fichier header - tous en un]]></comment>
		<comment lang="de"><![CDATA[Installation Headerdatei - Alles in einem]]></comment>
 	         <find><![CDATA[</head>]]></find>
		<action type="before-add"><![CDATA[<!-- INCLUDE dkp/dkpheader.html -->]]></action>
	</edit>
</open>

with this in the file : note the anchors. thats needed for plugins writing in the header.

Code: Select all

<!-- BBDKP -->
<script type="text/javascript" src="{T_TEMPLATE_PATH}/dkp/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="{T_TEMPLATE_PATH}/dkp/jquery.tools.min.js"></script>
<script type="text/javascript" src="{T_TEMPLATE_PATH}/dkp/jquery.progressbar.js"></script>

<!-- BBTIPS -->
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5859
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: Avoiding multiple jQuery files?

Post by MattF »

king577 wrote:Hi VSE,

Have you tried the last code I published?
Yes I did, and it was still the same issue. :cry:
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5859
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: Avoiding multiple jQuery files?

Post by MattF »

I found a way to have success, although I'm not sure if it's hackish or not, or even the best way to write this:

If I use king's method for both jQuery AND the script I want to use, then it works out.

Code: Select all

<script type="text/javascript">
//test for jQuery presence, and include it in HEAD if not found
if (typeof jQuery == 'undefined'){
   var jscript = document.createElement('script');
   jscript.setAttribute("type","text/javascript");
   jscript.setAttribute("src","{T_TEMPLATE_PATH}/jquery.js");
   jscript.setAttribute("charset","utf-8");
   document.getElementsByTagName('head')[0].appendChild(jscript);
}

//now include my script in the head
var script = document.createElement('script');
script.setAttribute("type","text/javascript");
script.setAttribute("src","{T_TEMPLATE_PATH}/my_script.js");
script.setAttribute("charset","utf-8");
document.getElementsByTagName('head')[0].appendChild(script);
</script>
Also, this is best placed at the footer, since you do not know where in the entire page/DOM that another MOD may be already including jQuery. So this test needs to run after the whole page has loaded. However, the scripts referenced here will be dynamically added to the HEAD.

So, this seems to work for me. I have tried it on pages both with and without 3rd party MODs including jQuery with success. Thanks to king577 for the help on this! If anybody has any ideas to clean up or optimize this code, please post 8-)
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5859
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: Avoiding multiple jQuery files?

Post by MattF »

Jeepers. So although the above code I posted works - it is unreliable.

I have seemed to experience that, at least on a first load, it won't work. I am guessing this is because jQuery, which is around 90kB, needs to be fully downloaded before anything else using it will work. And the above methods just won't work on the first run. On subsequent runs, when jQuery is already cached, it seems to work fine then.

I guess this means it is back to the traditional way of linking to jQuery and just hoping other users can handle any potential conflicts at their own risk.

PS: Even if phpBB were not to include jQuery as part of the package, I really do think it would be smart - even as soon as the next release - to include a universal location for 3rd party javascript libraries (Dojo, Motools, jQuery, etc). Simply a single folder in phpBB where MOD authors will put these libraries if they write MODs that use them.
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: Avoiding multiple jQuery files?

Post by igorw »

VSE wrote:Simply a single folder in phpBB where MOD authors will put these libraries if they write MODs that use them.
Yes, we want this. Proposals are welcome (@area51).
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three
User avatar
AGC
Registered User
Posts: 292
Joined: Sat Sep 15, 2007 10:26 pm
Contact:

Re: Avoiding multiple jQuery files?

Post by AGC »

igorw wrote:
VSE wrote:Simply a single folder in phpBB where MOD authors will put these libraries if they write MODs that use them.
Yes, we want this. Proposals are welcome (@area51).
But then this issue
"Remote JS and MOD update policy changes" is come up.
(phpBB 2.0.x): []Admin Topics List[] MOD. << Description & Download. , []Highlight Author[] MOD.

Donate-paypal
(phpBB 3.0.x)[ver] MODs: [RC1] >Import old *.pak files MOD. + . . .[8] > ACP - Modules Quick Access (MQA) MOD. + . . . [7-PL1] > Multi Smile (actions) MOD
User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 18282
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ »

How is that an issue? The solution to the problem here would just NOT involve remote JS inclusion.
Apply to become a Jr. Extension Validator
My extensions | In need of phpBB services? | Was I helpful today?
No unsolicited PMs unless you're planning on asking for paid help.
User avatar
AGC
Registered User
Posts: 292
Joined: Sat Sep 15, 2007 10:26 pm
Contact:

Re: Avoiding multiple jQuery files?

Post by AGC »

DavidIQ wrote:How is that an issue? The solution to the problem here would just NOT involve remote JS inclusion.
The issue about the remote JS. That's what his suggesting.
VSE wrote:... - to include a universal location for 3rd party javascript libraries (Dojo, Motools, jQuery, etc). Simply a single folder in phpBB where MOD authors will put these libraries if they write MODs that use them.
(phpBB 2.0.x): []Admin Topics List[] MOD. << Description & Download. , []Highlight Author[] MOD.

Donate-paypal
(phpBB 3.0.x)[ver] MODs: [RC1] >Import old *.pak files MOD. + . . .[8] > ACP - Modules Quick Access (MQA) MOD. + . . . [7-PL1] > Multi Smile (actions) MOD
User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 18282
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ »

Oh I see...forgot to update that other topic. Fixed. ;) Besides what VSE is suggesting has nothing to do with externally hosted scripts.
Apply to become a Jr. Extension Validator
My extensions | In need of phpBB services? | Was I helpful today?
No unsolicited PMs unless you're planning on asking for paid help.
User avatar
AGC
Registered User
Posts: 292
Joined: Sat Sep 15, 2007 10:26 pm
Contact:

Re: Avoiding multiple jQuery files?

Post by AGC »

His suggesting that phpBB hosting these files, so users could download.
(phpBB 2.0.x): []Admin Topics List[] MOD. << Description & Download. , []Highlight Author[] MOD.

Donate-paypal
(phpBB 3.0.x)[ver] MODs: [RC1] >Import old *.pak files MOD. + . . .[8] > ACP - Modules Quick Access (MQA) MOD. + . . . [7-PL1] > Multi Smile (actions) MOD
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5859
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: Avoiding multiple jQuery files?

Post by MattF »

AGC wrote:His suggesting that phpBB hosting these files, so users could download.
No, all I am suggesting is the phpBB come up with a standard way for MODs to deal with these files if they want to use them... Sort of like we have a "hooks" folder for hooks... So we should have a javascript folder to keep javascripts in - in one place.
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
RMcGirr83
Former Team Member
Posts: 22011
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Avoiding multiple jQuery files?

Post by RMcGirr83 »

So we should have a javascript folder to keep javascripts in - in one place.
I am not sure how that would help in not having two, eg, jQuery libraries loaded on a board at the same time.
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 18282
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ »

RMcGirr83 wrote:
So we should have a javascript folder to keep javascripts in - in one place.
I am not sure how that would help in not having two, eg, jQuery libraries loaded on a board at the same time.
The JavaScript files would be organized and you would be able to tell which libraries are already present and not upload the same ones again into some other folder specified by the MOD. Of course this might be a moot point with the hooks present but nonetheless a good suggestion.
Apply to become a Jr. Extension Validator
My extensions | In need of phpBB services? | Was I helpful today?
No unsolicited PMs unless you're planning on asking for paid help.
User avatar
RMcGirr83
Former Team Member
Posts: 22011
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: Avoiding multiple jQuery files?

Post by RMcGirr83 »

The JavaScript files would be organized and you would be able to tell which libraries are already present and not upload the same ones again into some other folder specified by the MOD.
Even if the mod is installed manually? How would a user be able to "tell"?
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 18282
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ »

Well if the JavaScript library was there the user manually uploading the files would see that the file is already there and even if they didn't see it their FTP program would tell them.
Apply to become a Jr. Extension Validator
My extensions | In need of phpBB services? | Was I helpful today?
No unsolicited PMs unless you're planning on asking for paid help.
Locked

Return to “[3.0.x] MOD Writers Discussion”