Avoiding multiple jQuery files?

Discussion forum for MOD Writers regarding MOD Development.
User avatar
VSE
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 4484
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Avoiding multiple jQuery files?

Post by VSE » Sat Mar 05, 2011 7:30 pm

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

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

Re: Avoiding multiple jQuery files?

Post by DavidIQ » Sat Mar 05, 2011 8:06 pm

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 » Sat Mar 05, 2011 10:56 pm

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: 9897
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 » Sat Mar 05, 2011 11:37 pm

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

Re: Avoiding multiple jQuery files?

Post by VSE » Sun Mar 06, 2011 12:42 am

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

User avatar
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 23722
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Avoiding multiple jQuery files?

Post by Paul » Sun Mar 06, 2011 10:14 am

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.
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

king577
Registered User
Posts: 60
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by king577 » Sun Mar 06, 2011 2:17 pm

JavaScript code:

Code: Select all

<script type="text/javascript">
if (typeof $ != 'function')
    alert("jquery don't exists");
else
    alert("jquery exists");
</script>
Sorry for the mistakes I do from time to time in English. This is not my native language. I am trying to improve my English, so please notify me about any mistakes I make. Thanks! :)

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

Re: Avoiding multiple jQuery files?

Post by VSE » Sun Mar 06, 2011 5:05 pm

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

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

Re: Avoiding multiple jQuery files?

Post by VSE » Sun Mar 06, 2011 7:41 pm

Actually the above code causes XHTML errors :(
Official phpBB Extensions My Extensions & MODs
Please do not PM me for support.
Dictated but not read.

king577
Registered User
Posts: 60
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by king577 » Sun Mar 06, 2011 7:58 pm

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>
Sorry for the mistakes I do from time to time in English. This is not my native language. I am trying to improve my English, so please notify me about any mistakes I make. Thanks! :)

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

Re: Avoiding multiple jQuery files?

Post by VSE » Sun Mar 06, 2011 8:54 pm

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

User avatar
Marc
Development Team Leader
Development Team Leader
Posts: 5234
Joined: Tue Oct 30, 2007 10:57 pm
Location: Munich, Germany
Name: Marc
Contact:

Re: Avoiding multiple jQuery files?

Post by Marc » Mon Mar 07, 2011 1:02 pm

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).
Quickedit for phpBB 3.1
I'm available for custom work - just send me a PM for a quote.

king577
Registered User
Posts: 60
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by king577 » Mon Mar 07, 2011 3:35 pm

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>
Sorry for the mistakes I do from time to time in English. This is not my native language. I am trying to improve my English, so please notify me about any mistakes I make. Thanks! :)

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

Re: Avoiding multiple jQuery files?

Post by VSE » Mon Mar 07, 2011 5:23 pm

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

king577
Registered User
Posts: 60
Joined: Mon Mar 01, 2010 7:28 pm

Re: Avoiding multiple jQuery files?

Post by king577 » Mon Mar 07, 2011 5:59 pm

Hi VSE,

Have you tried the last code I published?
Sorry for the mistakes I do from time to time in English. This is not my native language. I am trying to improve my English, so please notify me about any mistakes I make. Thanks! :)

Locked

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

Who is online

Users browsing this forum: No registered users and 18 guests

cron