Avoiding multiple jQuery files?

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

Re: Avoiding multiple jQuery files?

Post by Sajaki » Mon Mar 07, 2011 7:22 pm

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
VSE
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 4675
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: Avoiding multiple jQuery files?

Post by VSE » Tue Mar 08, 2011 2:14 am

king577 wrote:Hi VSE,

Have you tried the last code I published?
Yes I did, and it was still the same issue. :cry:
Dictated but not read.
Official phpBB Extensions My Extensions & MODs
Please do not PM me for support.

User avatar
VSE
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 4675
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: Avoiding multiple jQuery files?

Post by VSE » Tue Mar 08, 2011 4:52 am

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-)
Dictated but not read.
Official phpBB Extensions My Extensions & MODs
Please do not PM me for support.

User avatar
VSE
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 4675
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: Avoiding multiple jQuery files?

Post by VSE » Tue Mar 08, 2011 5:17 pm

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.
Dictated but not read.
Official phpBB Extensions My Extensions & MODs
Please 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 » Tue Mar 08, 2011 8:40 pm

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 » Mon Mar 14, 2011 10:07 pm

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: 16720
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ » Mon Mar 14, 2011 10:57 pm

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 » Tue Mar 15, 2011 1:27 am

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: 16720
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ » Tue Mar 15, 2011 1:36 am

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 » Tue Mar 15, 2011 1:51 am

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
VSE
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 4675
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: Avoiding multiple jQuery files?

Post by VSE » Tue Mar 15, 2011 4:44 am

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.
Dictated but not read.
Official phpBB Extensions My Extensions & MODs
Please do not PM me for support.

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 20934
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Avoiding multiple jQuery files?

Post by RMcGirr83 » Tue Mar 15, 2011 10:04 am

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.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 16720
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ » Tue Mar 15, 2011 11:19 am

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
Recognised Extension Developer
Posts: 20934
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Avoiding multiple jQuery files?

Post by RMcGirr83 » Tue Mar 15, 2011 1:45 pm

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"?
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 16720
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón
Contact:

Re: Avoiding multiple jQuery files?

Post by DavidIQ » Tue Mar 15, 2011 2:33 pm

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”

Who is online

Users browsing this forum: No registered users and 14 guests