Best Served Cold

Web design and development

We pride ourselves on producing high quality websites that fully conform to the highest web standards. Our sites are accessible and search engine friendly, making sure that all our sites have the best possible chance of succeeding where so many others fail.

Embedding PHP in SSJS pages

Probably not required by many, but I was working on a site recently which had been built totally in Server Side JavaScript (SSJS). It took me about an hour to work what SSJS is to begin with! But it appears that Server-side JavaScript refers to JavaScript that executes on the server rather than client side JavaScript (CSJS). Quite old and not used frequently, the first implementation was Netscape’s LiveWire which was used in the Enterprise Server 2.0 released in 1996.

The key advantage of this setup is for asynchronous interaction with the server (AJAX), however ASP and PHP can handle this type of interaction sufficiently now-a-days.

The client I was working for had most of their system set up in SSJS, but had a WordPress implementation on the same server using PHP and the client wanted to display their articles on their pages. So the issue I had was using two languages in the same page. After a day of fiddling around and working out the syntax of SSJS, I came up with this:

<% Response.Buffer = true ; var objXMLhttp = Server.CreateObject('Microsoft.XMLHTTP') ; var URL = "http://www.yourdomain.com/yourFile.php" ; objXMLhttp.Open("GET", URL, false) ; objXMLhttp.Send() ; if (objXMLhttp.status != 200) { // fire some sort of error exception } else { Response.Write(objXMLhttp.responseText) ; } %>

To briefly explain, using XMLHTTP, we create an object using the URL reference, if there’s an error (..status != 200), spit something out, otherwise display the content. You could of course close the object at the end of the script for good measure.
So you can call in any PHP or ASP file and plonk it in your SSJS page. You could of course do the same thing with a little JavaScript, but that wouldn’t show up in the source code of the page and would be very bad SEO (Search Engine Optimisation).