Page 1 of 3

Avoiding multiple jQuery files?

Posted: Sat Mar 05, 2011 7:30 pm
by VSE
With more and more MODs making use of jQuery, has anyone come up with a way to prevent having a build up of redundant links to jquery,js in the overall_header.html?

Potential problems are people adding MODs could have multiple links to jquery.js files in their headers, or links to different versions of jquery.js files too.

Is there any idea how to prevent or notify admin's installing MODs how to make sure they only have one jquery file, even though they may have installed multiple MODs that use and include jQuery??

Re: Avoiding multiple jQuery files?

Posted: Sat Mar 05, 2011 8:06 pm
by DavidIQ
There are no good ways of preventing that but we are open to ideas and suggestions ;)

Re: Avoiding multiple jQuery files?

Posted: Sat Mar 05, 2011 10:56 pm
by AGC
The author of the MOD could add a find the line with the jQuery and delete it,
and then add his line to the overall_header.html.

Re: Avoiding multiple jQuery files?

Posted: Sat Mar 05, 2011 11:37 pm
by Erik Frèrejean
Adding a comment about it with the find is probably a more logical opion (at least that is the I'd do it).
If you already have jQuery added to your board, than you can skip this step.

Re: Avoiding multiple jQuery files?

Posted: Sun Mar 06, 2011 12:42 am
by VSE
Yes, adding a comment is the best option today (plus big warnings in the MODX file). Unfortunately that does help AutoMOD users.

I wonder, are there any: If jQuery already exists logic functions we could employ?

Re: Avoiding multiple jQuery files?

Posted: Sun Mar 06, 2011 10:14 am
by Paul
VSE wrote:I wonder, are there any: If jQuery already exists logic functions we could employ?
as far I know there is no real way to detect if jquery exists.

Re: Avoiding multiple jQuery files?

Posted: Sun Mar 06, 2011 2:17 pm
by king577
JavaScript code:

Code: Select all

<script type="text/javascript">
if (typeof $ != 'function')
    alert("jquery don't exists");
else
    alert("jquery exists");
</script>

Re: Avoiding multiple jQuery files?

Posted: Sun Mar 06, 2011 5:05 pm
by VSE
I actually came up with this idea:

Code: Select all

<script type="text/javascript">
if (typeof jQuery == 'undefined')
    document.write('<script type="text/javascript" src="{T_TEMPLATE_PATH}/jquery.js" charset="utf-8"><\/script>');
</script>
This will only include jquery now, if it does not already exists.

If every MOD authour took this approach when using jquery, then the library would only be included once :D

Re: Avoiding multiple jQuery files?

Posted: Sun Mar 06, 2011 7:41 pm
by VSE
Actually the above code causes XHTML errors :(

Re: Avoiding multiple jQuery files?

Posted: Sun Mar 06, 2011 7:58 pm
by king577
I think the XHTML errors are caused by using document.write.
The following code might work.

Code: Select all

<script type="text/javascript">
if (typeof jQuery == 'undefined'){
   var script = document.createElement('script');
   script.setAttribute("type","text/javascript");
   script.setAttribute("src","{T_TEMPLATE_PATH}/jquery.js");
   script.setAttribute("charset","utf-8");
   document.getElementsByTagName('head')[0].appendChild(script);
}
</script>

Re: Avoiding multiple jQuery files?

Posted: Sun Mar 06, 2011 8:54 pm
by VSE
It seems only problem with that code, is it gets added right before the closing HEAD tag.

jQuery needs to be called before any other scripts that may use it, so is there a way to append it right after the opening HEAD tag instead??

Re: Avoiding multiple jQuery files?

Posted: Mon Mar 07, 2011 1:02 pm
by Marc
You could use prepend for that but there are also other issues when you just check for the existence of jQuery.

I've had issues with MODs that do not use a full jQuery library. So just checking for jQuery probably won't do it.

IMO the only real fix for this would be to include jQuery by default in phpBB (which probably won't happen before phpBB 3.2).

Re: Avoiding multiple jQuery files?

Posted: Mon Mar 07, 2011 3:35 pm
by king577
VSE wrote:It seems only problem with that code, is it gets added right before the closing HEAD tag.

jQuery needs to be called before any other scripts that may use it, so is there a way to append it right after the opening HEAD tag instead??
I used the method appendChild. In the following code I am using the method insertBefore.

Usually, this is the first line inside the <head> tag in the template overall_header.html:
<meta http-equiv="content-type" content="text/html; charset={S_CONTENT_ENCODING}" />

So, the script finds the first meta element in page
var meta = document.getElementsByTagName('meta')[0];
And then add the link to jquery.js
document.getElementsByTagName('head')[0].insertBefore(script,meta);

I hope it works.

Code: Select all

<script type="text/javascript">
if (typeof jQuery == 'undefined'){
   var meta = document.getElementsByTagName('meta')[0];
   var script = document.createElement('script');
   script.setAttribute("type","text/javascript");
   script.setAttribute("src","{T_TEMPLATE_PATH}/jquery.js");
   script.setAttribute("charset","utf-8");
   document.getElementsByTagName('head')[0].insertBefore(script,meta);
}
</script>

Re: Avoiding multiple jQuery files?

Posted: Mon Mar 07, 2011 5:23 pm
by VSE
OK this is weird... I actually got your first code to work, king... But it only worked if I also added an alert message to the end of it!?!

It is as if adding an alert gave the browser time to load jquery itself, before reaching the script that needed jquery. But without the alert, the javascript all happens to quickly and the script is loaded before jquery itself could be fully loaded. :(

I had something like this, and it was in the footer:

Code: Select all

<script type="text/javascript">
if (typeof jQuery == 'undefined'){
   var script = document.createElement('script');
   script.setAttribute("type","text/javascript");
   script.setAttribute("src","{T_TEMPLATE_PATH}/jquery.js");
   script.setAttribute("charset","utf-8");
   document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
<script type="text/javascript" src="{T_TEMPLATE_PATH}/myscript.js" charset="utf-8"></script>
I'm beginning to think that marc's suggestion is the only real answer here: phpBB needs to include jquery, and give us an ACP option to turn it on or off if we want to use it. :roll:

Re: Avoiding multiple jQuery files?

Posted: Mon Mar 07, 2011 5:59 pm
by king577
Hi VSE,

Have you tried the last code I published?