Syntax Basics

SalsaScript is written in files with the extension ".sjs", for Serverside JavaScript. SJS files are HTML files with embedded tags for SalsaScript code. (Don't like code inside HTML files?)

SalsaScript code is distinguished from standard HTML in two ways:
<? and ?> tags delimit blocks of SalsaScript and
<?=value?> blocks writes a value to the page. For example, the following block calculates 3+3, and displays the result:

<? var x = 3 + 3; ?>
<?= x ?>
will return to the browser nothing but the value:
6

You can also use the 'print' method to print a value directly to the page

<?
    var x = 3 + 3;
    print(x);
?>
results in
6

All Javascript standard methods and code blocks are supported, using standard brackets.

<? for (var i = 0; i < 10; i++) { ?>
    <?= i ?>
<? } ?>
results in

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

Just as in normal Javascript, you can also create functions dynamically, and call them inline

<?
    // This function has embedded HTML and Javascript
    function displayList(number) { ?>
        <li><?= number ?>.
    <? }

    //The page loops through 10 numbers, and calls the displayList function for each
    for (var i = 0; i < 10; i++) {
        displayList(i);
    }
?>
results in
  • 0.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • Common Functions

    SalsaScript comes with a variety of common functions to grant access to different parts of an application.

    URL Parameters

    You can retrieve parameters from the URL using a salsa.getParameter method call.

    <?
    
    var x=salsa.getParameter("name");
    
    print(x);
    ?>
    

    getParameter will always return an object with a length, even if it does not exist, so to test if a value is passed, use the ".length" attribute.

    <?
    
    var x=salsa.getParameter("name");
    
    if (x.length>0){
    	?>Your parameter was <?=x?><?
    }else{
    	?>A name was not specified.<?
    }
    
    ?>
    

    Sometimes the existence of a parameter is important, regardless if it has a value or not. To test for the presence of a parameter, use the 'hasParameter' method, which will return back true or false if a parameter is present or not.

    <?
    
    //will return true for url 'mypage.sjs?name'
    if (Request.hasParameter("name")){
    	?>There is a 'name' parameter<?
    }else{
    	//will return false for url 'mypage.sjs'
    	?>There is not a 'name' parameter<?
    }
    
    ?>
    

    Request.forward and salsa.sendRedirect Syntax

    salsaScript allows you to use the jsp .forward and .sendRedirect actions.

    Forward

    • a forward is performed internally on the server
    • the browser is completely unaware that it has taken place, so its original URL remains intact
    • any browser reload of the resulting page will simple repeat the original request, with the original URL

    	Request.forward("/api/action/processAction.jsp");
    
    will return to the browser nothing but the value:

    Redirect

    • a redirect is a two step process, where action instructs the browser to fetch a second URL, which differs from the original
    • a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
    • objects placed in the original request scope are not available to the second request

    	salsa.sendRedirect("/api/action/processAction.jsp");
    

    When using a redirect, the server sends back an HTTP respons status of 302.

    HTTP/1.1 302 Object moved
    Location: domain/newlocation.jsp
    

    Baked in XML

    The Javascript standard incorporates XML as a first class citizen, meaning that XML can be included inline with Javascript/SalsaScript code. It can also be used in
    <?
    
    var myxml=<data organization_KEY="1">
      <event>
        <item>
          <event_KEY>23</event_KEY>
          <Event_Name>Team Meeting</Event_Name>
        </item>
        <item>
          <event_KEY>24</event_KEY>
          <Event_Name>House Party</Event_Name>
        </item>
        <count>2</count>
      </event>
    </data>
    
    
    for each(event in myxml.event.item){
    	?><li><?=event.Event_Name?></li>
    <?
    }
    
    ?>
    
    results in:
  • Team Meeting
  • House Party
  • XML data can be used inline, or created from a String using the XML constructor

    <?
        var xmlString="<item><Event_Name>Team Meeting</Event_Name></item><item>"
        	+ "<event_KEY>24</event_KEY><Event_Name>House Party</Event_Name></item>";
        var xmlObject=new XML(xmlString);
    ?>
    

    Note: The SalsaScript interpreter requires that new XML not contain an xml tag (), and start with a '<';

    The Crawler Object

    In the new server to server world, it's often useful to pull content dynamically from a webpage. SalsaScript provides the 'crawler' object to simplify retrieving data.

    Getting Web pages

    An HTTP GET request is useful for retrieving data from RESTful API services, or other web pages you want to parse the content of, such as an RSS feed. Parameters for a GET request are appended to the end of

    <?
    var content = crawler.get("http://sandbox.salsalabs.com/dia/info.jsp?name=value");
    ?>
    

    Getting XML feeds

    A common use of the GET command is to retrieve a RSS feed, or other XML entities. These pages are often prefixed with a <?xml?> tag, that is not handled by Javascript. The crawler.getXML(url) method retrieves the XML, and removes errant characters, to return back a valid Javascript XML object.

    <?
    var content = crawler.getXML("http://rss.cnn.com/rss/cnn_topstories.rss");
    ?>
    

    Posting to a URL

    An HTTP POST request is useful if you're posting account information, or information you don't want to appear in logs. Post parameters are contained in a Javascript object as the second parameter.

    <?
    var content = crawler.post("http://rss.cnn.com/rss/cnn_topstories.rss",
        {myparam:'myvalue',myparam2:'myvalue2'});
    ?>
    

    crawler.get + XML = Snappy

    Combining the crawler object and standard XML is a fast and easy way to pull and render XML content to the screen. For example, the following few lines of code pulls down a current RSS feed from CNN, parses it as XML, then loops through all the top stories, and prints them to the screen.
    <h3>Top Stories</h3>
    <?
    var xmlContent = crawler.getXML("http://rss.cnn.com/rss/cnn_topstories.rss");
    
    for each(story in xmlContent.channel.item){
        ?><li><a href='<?=story.link?>'><?=story.title?></a></li>
        <?
    }
    
    ?>