Alex Tape
Alex Tape

Reputation: 2291

TypoScript: Check if JS/CSS File is already included

anybody knows how to check if a js/css file is already included with typoscript?

Example

[Template_A.ts]

page.includeJS {
  jsfile = fileadmin/Template/js/jquery-1.10.2.min.js
}

now if i got an extension with the same include e.g.

[Extension_A.ts]

page.includeJS {
  jsfile = fileadmin/Template/js/jquery-1.10.2.min.js
}

Is there a way to prevent this kind of double code injection? Maybe i got another Template e.g. Template_B.ts where jquery is not included - than the Extension_A.ts has to include jquery by itself.

Kinldy

Upvotes: 1

Views: 683

Answers (2)

pgampe
pgampe

Reputation: 4578

You can use the same key inside includeJS such it just gets overridden if you include the file twice.

Other than that you should put jQuery into includeJSlibs, such that it is loaded before the other JS files.

Other than that, the TS should be unique for each page. Therefore you always to make sure anyway that all resources are included in-order. You should not include JS libs with the automatic extension TS setups. Use your documentation to tell the integrator what needs to be included and what not.

Upvotes: 2

peter_the_oak
peter_the_oak

Reputation: 3710

The various and independent inclusion of scripts by plugins and templates is one of the tricky points in TYPO3. As far as I know, this point cannot be managed at one single point.

There is a plugin "t3jquery" that offers to build, compress and share a common jQuery library. It also has a service to analyze other plugins for their dependencies. But this doesn't solve the problem in general, as many plugins don't check for libraries already loaded.

The most stable way is to remove all plugin's references to libraries manually in your TypoSkript. This gives you some simple additional TypoSkript lines. I use lines like these:

plugin.tx_imagecycle_pi1.file.jQueryLibrary >
plugin.tx_imagecycle_pi1.jQueryLibrary >
plugin.tx_multicontent_pi1.file.jQueryLibrary > 
plugin.tx_multicontent_pi1.jQueryLibrary > 
# Fluid
page.headerData.998 >

You can find the matching TypoSkript descriptors by searching for the library name in the TypoSkript browser or by greping in the plugin's source code. You will also need this if you wish to add libraries as part of content that was get by AJAX, thus separating the libs from the page content.

Here's a tut (in German): http://jweiland.net/typo3/typoscript/javascript-manuell-entfernen-oder-einbinden.html

Futher possibilities you can check:

  • Some plugins are written in good structure and offer to keep back their Javascript in the settings.
  • Some script inclusions may come rather from the static template but from a plugin, so don't forget to have a look there.

Upvotes: 1

Related Questions