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:

Workaround
<form action="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%"> 
<input type="text" name="FridayLast" size="20" value="" id="FridayLast" />
<input type="submit" value="Go">
</form>

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

<script>
   var today = new Date(); 
   var el = document.getElementById('FridayLast');
   
   today.setDate(today.getDate()+1);  
      
    while (today.getDay()!=5){  
       today.setDate(today.getDate()-1);  
    }  

    el.value = today.toDateString();  
</script>

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.

E.g.

<script>
  alert("SCRIPTURLPATH is =" + foswiki.getPreference("SCRIPTURLPATH"));
</script>

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:

<script>
  alert("SCRIPTURLPATH is =" + foswiki.getPreference("FRIDAYLAST", true));
</script>

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 http://plugins.jquery.com/project/query-object

-- MichaelDaum - 08 Mar 2011

QuestionForm edit

Subject Topic Markup Language and applications
Extension
Version TWikiRelease04x02x04
Status Answered
Topic revision: r2 - 08 Mar 2011, MichaelDaum
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy