Forums / Developer / Add a javascript in the head through a module?

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