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
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 (salsa.hasParameter("name")){
?>There is a 'name' parameter<?
}else{
//will return false for url 'mypage.sjs'
?>There is not a 'name' parameter<?
}
?>
salsaScript allows you to use the jsp .forward and .sendRedirect actions.
salsa.forward("/api/action/processAction.jsp");
will return to the browser nothing but the value:
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
<?
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:
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 '<';
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.
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://salsa.democracyinaction.org/dia/info.jsp?param=myparam");
?>
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");
?>
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'});
?>
<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>
<?
}
?>