New Foswiki release 2.1.6 is available with important security fixes.
Sourceforge foswiki email lists being discontinued. Subscribe to the new Foswiki announce and discuss lists at MailingLists
This question about Topic Markup Language and applications: Answered

Set a wiki variable using JavaScript

I want to set some wiki variables using JavaScript.

I know how to use JS to modify HTML tags. So (for example) I've done this:

<input type="text" name="FridayLast" size="20" value="" id="FridayLast" />
<input type="submit" value="Go">

   * Set FridayLast = %URLPARAM{"FridayLast"}%

   var today = new Date(); 
   var el = document.getElementById('FridayLast');
    while (today.getDay()!=5){  

    el.value = today.toDateString();  

But that takes an extra click to submit the form and come back around with the values in %URLPARAM{}%.

Is there a simple and clean way to say (pseudocode)
   * Set Variable = JavaScriptFunction(...)

-- VickiBrown - 07 Mar 2011

That's not possible that way and most probably the wrong approach, because the namespace of variables is on the server whereas javascript is executed by the browser.

However there are means to drag in values from the server's namespace of variables into javascript. The primary interface to do so is the foswiki.getPreference() to fetch a foswiki preference variable.


  alert("SCRIPTURLPATH is =" + foswiki.getPreference("SCRIPTURLPATH"));

This will fetch the value of the preference variable "SCRIPTURLPATH" and display it's value in a popup. The value of preference variables is either added already to the page as <meta name="..." content="..." > tags, or if not present fetched from the server using an ajax call:

  alert("SCRIPTURLPATH is =" + foswiki.getPreference("FRIDAYLAST", true));

This example set the second parameter of getPreferences(varName, userServer) to true so that it really will do the ajax call instead of returning null.

There's a list of variables stored in EXPORTEDPREFERENCES that are added to every page using meta tags and thus don't need the ajax overhead to fetch it. If you happen to need a certain non-standard preference variable in your javascript on every page, then it makes total sense to add this variable to the EXPORTEDPREFERENCES list.

Another way to expand any TML expression by the server while you are in javascript is to make use of one of the REST handlers in RenderPlugin. Have a look at the docu there to get an idea how to fetch more complex TML expressions than only a predefined preference variable.

In your example above you are trying to get the URLPARAM and incorporate it into your application. There are actually plain javascript means to read these in.

There's a jQuery plugin comming with JQueryPlugin for more complex operations on the query string, called querystring. See the docu at

-- MichaelDaum - 08 Mar 2011

QuestionForm edit

Subject Topic Markup Language and applications
Version TWikiRelease04x02x04
Status Answered
Topic revision: r2 - 08 Mar 2011, MichaelDaum - This page was cached on 11 Mar 2018 - 15:46.

The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License