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).

Comments

Name (required)

Email (required)

Website

Leave a comment

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

categories

news

Nice day for it!

Our latest client site is launched today - Lennox Rods.

Lennox produce travel fishing rods to the highest quality and will make a bespoke, hand crafted specialist fishing rod for any type of fishing you plan on doing - Trout, Salmon, Blue Water and others.

Lennox Rods

Fully Flared

I think I might have missed this somewhere over the last year but I love the intro to this skateboarding video. Directed by Spike Jonze with music from M83, it looks incredible and the music fits perfectly.

video after the jump

Slusho!

You can’t drink just six apparently. So, this was thought to be a marketing plot for Cloverfield (see our 11808 update but now it’s appeared in Heroes and there are rumours of a Lost appearance this season.

read more