Forums / Setup & design / using Javascript with eZ

using Javascript with eZ

Author Message

Claudia Kosny

Friday 05 January 2007 3:46:58 am

Hi there

I am looking for tips on how to use Javascript with eZ. I have two, pretty similar, problems:

I use Ajax to reload some parts of a page. To do this I need the url of the server script that is to be called by the Ajax request. The problem is that if I follow the motto of keeping design and functionality apart, my whole Javascript is in an external js file. But in this file I do not have the option to call ezurl() to create a full url, so I have to hardcode the url which can be quite annoying if you move servers. Another option would be to add a script block in the pagelayout.tpl where I add these urls as global variables, so the scripts in the external files can see them always. But this quite error prone as the code for executing one function is separated in several places and if I have lots of scripts, this gets very confusing.

Has anyone a better idea how to solve this?

A similar problem is having messages in the Javascript. As I cannot call the i18n operator, I cannot translate the messages properly.

Any ideas?

Claudia

Betsy Gamrat

Tuesday 09 January 2007 8:38:37 pm

Claudia,

I would recommend adding an init or config function in the external js file. That function would be called by script code in pagelayout.tpl that would set the variables you need.

Good luck,

Betsy

For example:

<b>pagelayout.tpl</b>

<script src="{'ajax.js'|ezdesign}"></script>
<script>init_ajax('{$node.url_alias|ezurl}',30)</script>

<b>ajax.js</b>

target_url=''
timeout=0

function init_ajax(in_url,timelimit)
{
  target_url=in_url
  timeout=timelimit
}

function do_ajax()
{
  sendRequest(target_url)  /* Sample code, may not be correct syntax/call */
}