Fog Creek Software
Discussion Board




Knowledge Base
Documentation
Terry's Tips
Darren's Tips

Passing a CD variable to javascript

Is there any way to use a CD variable, such as {$.fileName$} inside javascript? I can't get it to work with a simple script to count all files in specific folders that I would like to choose dynamically.

Dennis Harwood
Wednesday, January 28, 2004

Sure... lots of people on the board do this..

but you have to remember your CD variable will be published and SET.  Javascript won't be manipulating the VARIABLE anymore.. it will just be manipulating the VALUE of the variable at publish time.

Think about it this way. Write your javascript first, and where you want the cd variable to be, write "this is the filename" or something static.  Then replace that with the variable you want in CD.

What happens next:
1. You hit publish
2. CD replaces the variable with a string (the actual value of the variable at publish time)
3. Look at the HTML source of the file... does it make sense where you wrote the javascript?
4. Load it in the webbrowser and let javascript use the variable.

Michael H. Pryor
Wednesday, January 28, 2004

I know what you are saying, but my problem is this.  If I use a script such as this one:

  <script language="javascript" type="text/javascript">
  //<![CDATA[
  <!--
  var tmp = "{$ foreach x in (folder "/Articles/*") $}.{$ next $}";
  if (tmp.length==0)
  { document.write("<strong>Sorry, there are no articles yet. Check back soon.</strong>"); }
  else
  { document.write("We currently have ",tmp.length," articles in our database.<br>"); }
  //-->//]]>
  </script>

It works great.  But I want to change the folder using a CD variable, so the foreach command looks like:

var tmp = "{$ foreach x in (folder "/Articles/{$.FileName$}") $}.{$ next $}";

This won't work and I can't get any variation of it to work.  Any suggestions?

Dennis Harwood
Wednesday, January 28, 2004

That won't work because javascript is executed after cityscript has already run.

joel goldstick
Thursday, January 29, 2004

*  Recent Topics

*  Fog Creek Home