Avoiding multiple jQuery files?

Discussion forum for MOD Writers regarding MOD Development.
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

Avoiding multiple jQuery files?

Post by MattF »

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??
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
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 »

There are no good ways of preventing that but we are open to ideas and suggestions ;)
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 »

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.
(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
Erik Frèrejean
Former Team Member
Posts: 9899
Joined: Tue Oct 09, 2007 9:09 am
Location: The Netherlands, 3.0.x Support Forum
Name: Erik Frèrejean
Contact:

Re: Avoiding multiple jQuery files?

Post 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.
Support Toolkit | Support Request Template | Knowledge Base | phpBB 3.0.x documentation
I don't give support via PM or IM! (all unsolicited pms will be trashed!)
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 »

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?
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28616
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Avoiding multiple jQuery files?

Post 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.
Lenny1
Registered User
Posts: 66
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by Lenny1 »

JavaScript code:

Code: Select all

<script type="text/javascript">
if (typeof $ != 'function')
    alert("jquery don't exists");
else
    alert("jquery exists");
</script>
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 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
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 »

Actually the above code causes XHTML errors :(
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
Lenny1
Registered User
Posts: 66
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by Lenny1 »

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>
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 »

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??
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
Marc
Development Team Leader
Development Team Leader
Posts: 5657
Joined: Tue Oct 30, 2007 10:57 pm
Location: Munich, Germany
Name: Marc
Contact:

Re: Avoiding multiple jQuery files?

Post 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).
Lenny1
Registered User
Posts: 66
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by Lenny1 »

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>
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 »

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:
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
Lenny1
Registered User
Posts: 66
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by Lenny1 »

Hi VSE,

Have you tried the last code I published?
Locked

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