Page 2 of 3

Re: Avoiding multiple jQuery files?

Posted: Mon Mar 07, 2011 7:22 pm
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 -->

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 08, 2011 2:14 am
by VSE
king577 wrote:Hi VSE,

Have you tried the last code I published?
Yes I did, and it was still the same issue. :cry:

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 08, 2011 4:52 am
by VSE
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-)

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 08, 2011 5:17 pm
by VSE
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.

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 08, 2011 8:40 pm
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).

Re: Avoiding multiple jQuery files?

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

Re: Avoiding multiple jQuery files?

Posted: Mon Mar 14, 2011 10:57 pm
by DavidIQ
How is that an issue? The solution to the problem here would just NOT involve remote JS inclusion.

Re: Avoiding multiple jQuery files?

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

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 15, 2011 1:36 am
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.

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 15, 2011 1:51 am
by AGC
His suggesting that phpBB hosting these files, so users could download.

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 15, 2011 4:44 am
by VSE
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.

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 15, 2011 10:04 am
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.

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 15, 2011 11:19 am
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.

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 15, 2011 1:45 pm
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"?

Re: Avoiding multiple jQuery files?

Posted: Tue Mar 15, 2011 2:33 pm
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.