Using server-side JavaScript in a YQL open table you can shift JavaScript logic to the server side. In this example I wanted to write a script that loads an HTML document and counts the amount of times a certain tag is used. With pure JavaScript this can't be done because of cross-domain loading issues. This is why I wrote the following XML document and put it on my server:
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<sampleQuery>select * from count where url="http://www.korea-dpr.com/" and tag="strong"</sampleQuery>
<author>Christian Heilmann</author>
<documentationURL>http://isithackday.com/hackday-toolbox/yql-open-table-with-js</documentationURL>
<description>Counts to amount of times a certain tag is in an HTML document.</description>
</meta>
<bindings>
<select itemPath="" produces="XML">
<inputs>
<key id='url' type='xs:string' paramType='variable' required="true" />
<key id='tag' type='xs:string' paramType='variable' required="true" />
</inputs>
<execute><![CDATA[
var data = y.rest(url).get().response;
var match = new RegExp('<'+tag+'[>|\s]');
var count = data.split(match);
var number = parseInt(count.length-1);
response.object = <counted>
<url>{url}</url>
<tag>{tag}</tag>
<count>{number}</count>
</counted>;
]]></execute>
</select>
</bindings>
</table>
The y.rest() command loads external data for me on the server and then allows me to manipulate it in JavaScript. In this case, I get the content and split it at the tag I want to count. Then I return an object which is all the data as plain XML (as YQL supports E4X).
In YQL, you can apply this table with the use command:
use "http://isithackday.com/hackday-toolbox/yql-open-table-with-js/count-tags.xml" as count;
select * from count where url="http://www.korea-dpr.com/" and tag="strong"
The results would be:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2010-07-29T11:58:03Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url execution-time="3" proxy="DEFAULT"><![CDATA[http://isithackday.com/hackday-toolbox/yql-open-table-with-js/count-tags.xml]]></url>
<url execution-time="244" proxy="DEFAULT"><![CDATA[http://www.korea-dpr.com/]]></url>
<javascript execution-time="306" instructions-used="3095" table-name="count"></javascript>
<user-time>313</user-time>
<service-time>247</service-time>
<build-version>8771</build-version>
</diagnostics>
<results>
<counted>
<url>http://www.korea-dpr.com/</url>
<tag>strong</tag>
<count>904</count>
</counted>
</results>
</query>
See the source of this page to check how to use this in YUI3. Find out more about server-side JavaScript in YQL here.