Add a javascript in the head through a module?

Author Message

Douglas Hammond

Thursday 01 June 2006 11:27:40 am

I was thinking somehow inject it to JavaScriptList the below that is included in the head but don't know how to.

{section name=JavaScript loop=ezini( 'JavaScriptSettings', 'JavaScriptList', 'design.ini' ) }
<script language="JavaScript" type="text/javascript" src={concat( 'javascript/',$:item )|ezdesign}></script>
{/section}

is there a way I can inject a value into the ScriptList array in the module or from a template included in the template? I believe the latter is impossible.

Kristian Hole

Thursday 01 June 2006 11:49:50 am

Hi Douglas,

Welcome to the forum!

To insert a javascript, all you need to do, is add the following to settings/siteaccess/YOURUSERACCESS/design.ini.append.php

Where YOURUSERACCESS normally is plain,news, forum or similar.

[JavaScriptSettings]
JavaScriptList[]=yourjavascript.js

And put your javascriptfile in design/YOURDESIGN/javascript/yourjavascript.js

Where YOURDESIGN normally is plain,news,forum or similar.

http://ez.no/doc/ez_publish/technical_manual/3_8/concepts_and_basics/configuration/site_management

Kristian

http://ez.no/ez_publish/documenta...tricks/show_which_templates_are_used
http://ez.no/doc/ez_publish/techn...te_operators/miscellaneous/attribute

Douglas Hammond

Thursday 01 June 2006 11:56:37 am

Thanks for the quick reply.

I do do this, but I was wondering is there is another way?

I have some large scripts I only want loaded on some pages and do not want to have them load on all pages. I also don't want to do override temples just to add a new script to be included on those pages.

Any other suggestions?

I was hopeing somehow in the module at runtime I could add it to the ezini JavaScriptList without haveing to put it in the ini file.

Douglas Hammond

Friday 02 June 2006 2:07:56 pm

This is badly needed. If we can't can a standard way be discussed.

Tobias Struckmeier

Saturday 03 June 2006 10:19:49 am

Hi,

maybe you can try the following.

If you want to include this only in your own custom module you can use the navigationpart in your pagelayout.

Example:

{if eq( $navigation_part.identifier, 'yourCustomModuleNavigationpart' ) }
    <script language="JavaScript" type="text/javascript" src='yourjavascriptpathhere.js'}></script>
{/if}

Another way could be (eg. when you want it in the content views) to include a template in the pagelayout and make overrides for the pages/views where you want to have it. So you can use the flexible override system.

There may be also other ways those are the first i have in mind.

Xavier Dutoit

Sunday 04 June 2006 11:47:36 pm

Assuming you want to load the javascript based on something in the content (in the page_mainarea)

In your link.tpl/pagelayout.tpl
{cache-block keys=array(...)}
{section show=is_set($#extrajs)}
{$#extrajs}
{/section}
{/cache-block}

and in your view full

{set-block variable="extrajs" scope="global"}
<script ...
and other things you want to put in the header
{/set-block}

You are going to have to tune the cache/compile so it works on your case.

X+

http://www.sydesy.com

Kristian Hole

Tuesday 06 June 2006 1:00:20 am

In the pagelayout you can allways do something like:

{if $module_result.uri|eq('/mymodule/myview')}
 <script language="JavaScript" type="text/javascript" src={'myjs.js'|ezdesign}></script>
{/if}

This will include a javascript based on the uri of request.

If you are making your own module (otherwise ignore this), its probably better to set
$Result["javascript"]="myjs.js" in the end of the PHP code for the module, and then in the pagelayout use:

{if is_set($module_result.javascript)}
 <script language="JavaScript" type="text/javascript" src={$module_result.javascript|ezdesign}></script>
{/if}

Kristian

http://ez.no/ez_publish/documenta...tricks/show_which_templates_are_used
http://ez.no/doc/ez_publish/techn...te_operators/miscellaneous/attribute

Douglas Hammond

Tuesday 06 June 2006 1:46:05 pm

That's what i'll do, ill pass an array of js file in the Result. Thank you all for your ideas and help.

i now have in head

{section name=JavaScript loop=ezini( 'JavaScriptSettings', 'JavaScriptList', 'design.ini' ) }
<script language="JavaScript" type="text/javascript" src={concat( 'javascript/',$:item )|ezdesign}></script>
{/section}

{if is_set($module_result.javascript)}
{foreach $module_result.javascript as $script}
<script language="JavaScript" type="text/javascript" src={concat( 'javascript/',$script )|ezdesign}></script>
{/foreach}
{/if}

I think it should work

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 01:28:43
Script start
Timing: Jan 31 2025 01:28:43
Module start 'layout'
Timing: Jan 31 2025 01:28:43
Module start 'content'
Timing: Jan 31 2025 01:28:43
Module end 'content'
Timing: Jan 31 2025 01:28:43
Script end

Main resources:

Total runtime0.0255 sec
Peak memory usage6,144.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 588.2656151.2266
Module start 'layout' 0.00550.0045 739.4922220.7188
Module start 'content' 0.01000.0140 960.21091,009.9609
Module end 'content' 0.02400.0015 1,970.171941.9922
Script end 0.0254  2,012.1641 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002811.0890140.0002
Check MTime0.00114.2076140.0001
Mysql Total
Database connection0.00072.783210.0007
Mysqli_queries0.002710.575230.0009
Looping result0.00000.082410.0000
Template Total0.00104.110.0010
Template load0.00083.305410.0008
Template processing0.00020.793610.0002
Override
Cache load0.00062.248910.0006
General
dbfile0.00249.225780.0003
String conversion0.00000.034640.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs