Raw JavaScript Output
Return to Developer Support
There may be times you want to have a server-side JavaScript provide raw output rather than display content within a Layout. A common example of this is a dynamic RSS feed or XML feed.
Either the EditMe Scripted Page (text/vnd.editme.js.xml-page) or EditMe Script (text/vnd.editme.js) page types can provide raw output. Which one you use depends on whether you need to include other scripts. In most cases, the EditMe Scripted Page offers more flexibility.
To create a Raw Output script that returns "Hello world!" in plain text with no Layout, simply create a page with the Raw attribute set in Page Properties, set the MIME Type to EditMe Scripted Page, and insert the following content:
<script type="text/vnd.editme.js">
headers.put('content-type','text/plain');
server.print('Hello world!');
</script>
The exact same effect can be accomplished by changing the MIME type to EditMe Script and removing the <script> tag, leaving just the script content:
headers.put('content-type','text/plain');
server.print('Hello world!');
Setting Output Headers
Since the EditMe MIME type must be set to one of the script types, the MIME type sent to the browser must be over-ridden in these scripts using the headers.put('content-type','text/plain') call. You can also add arbitrary HTTP headers to be sent to the browser using the headers.put(headerName, value) method. "headers" is an implicit object of type java.util.Map.
Including Other Scripts
The primary reason an EditMe Scripted Page would be used here over just a EditMe Script is to include other scripts. For example, if you have a script shared by several other scripts on your site, you can include it using the process attribute command. Note that an enclosing <div> tag is added at the root to make it a valid XML document, but this is ignored in the script output.
In this example, a MySharedFunctions page, a page that contains a pageToXml() function to convert a Page object to XML, is processed before the local script runs, and the output is the requested page in XML format.
1. Create the MySharedFunctions page with MIME type set to EditMe Script and add the following content:
function pageToXml(p) {
return '<page name="' + p.name + '" version="' + p.version + '">'
+ '<![CDATA[' + p.content + ']]></page>';
}
2. Create the PageToXml page that includes the shared script and outputs the requested page to XML. The MIME type of this page should be EditMe Scripted Page. Note that the script must enforce its own page security.
<div>
<!-- include the shared functions script -->
<span editme="process 'MySharedFunctions'" />
<script type="text/vnd.editme.js">
// set the content type to XML
headers.put('content-type', 'text/xml');
// read the page name to return from the querystring
var requestedPageName = server.request('page');
if (requestedPageName=='') requestedPageName = 'Home';
// check this user's access to the requested page
var requestedPage = site.getPage(requestedPageName);
if (!requestedPage.canView) server.print('<error>Access denied</error>');
// if access is granted, return the page in xml
else server.print(pageToXml(requestedPage));
</script>
</div>
You can now view an XML representation of any page on your site by visiting http://YOURSITE.editme.com/PageToXml?page=PAGENAME.
