cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tobia Conforto <>
Subject Re: Using xsl variables in javascript
Date Wed, 13 Feb 2008 22:12:59 GMT
Lincoln Mitchell wrote:
> I simple need to convert this inline javascript:
> <script type="text/javascript">
>   function myFunction(){
>   <xsl:choose>
>     <xsl:when test=" $myParam='x'">
>       alert('x')
>     </xsl:when>
>   </xsl:choose>
>   }
> </script>
> …to an external javascript file like:
> function myFunction(){
>   if ($myParam='x'){
>     alert('x')
>   }
> }

I don't think you have execution times very clear.  XSLT code runs on  
your server at pipeline execution time, before or during page  
generation.  Client javascript code runs in the client browser at page  
rendering time, after page generation and transmission.  This means  
that whatever comes out of your pipeline must be well-formed HTML and/ 
or Javascript that will run on the browser, without access to any XSLT  
or Cocoon variables.  In fact, by the time the generated HTML reaches  
the browser, the variables used in your XSLT won't exist anymore.

Nevertheless, it might be that what you're after is simply this:

<script type="text/javascript">
   var myParam = "<xsl:value-of select="$myParam"/>";

Notice the extra quotation marks around the value-of: this XSLT code  
is generating a HTML script tag containing Javascript code that, when  
run by the browser, will declare a global string variable called  
myParam.  From that moment on, every piece of Javascript code run on  
the browser will have access to that global variable.

Also, this is generic web programming matter, not exactly related to  

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message