Archive for Category ‘Javascript‘

The Typer

I like to use “simple” text editors rather then word processors to write, I use TextMate on OSX and TextPad on Windows, adopting the OPML editor when i need to support and track structure. But when I saw Writeroom about a month ago I was struck with its beautiful design and essential feature set, I thought that it would have been great if such a kind of editor existed on the web (I was constantly using gmail drafts to jot down notes while not in my office). I have seen that many other around me are doing pretty much the same so having some time this month I did go on and build it myself.

I was planning to start development after I completed Tiber (a web feed reader I’m working on) but then the spreading of the “no frills editor” meme around the blogsphere (see here, here and here) made me change my plans and I suspended Tiber, while working on this writer tool.

Two weeks after The Typer was ready (have a look at the screencast!).

I have put it online with the hope to to show the work and to receive some feedback. If anyone wants to test it, just register your email on the site and i’ll send you an invitation (I’m doing this just to be able to handle the load gradually on the server, I’ll open up the service in september).

xml load and parse from javascript

I needed to load and parse some opml in a javascript: any recent browser (aka Mozilla, Firefox or IE6) has the abilty to asyncronously load a resource from the net (all the AJAX stuff is based on this), moreover of this is an xml file it is possible to have it parsed into a DOM object.
But the browsers i tested don’t parse correctly the content returned from the server if it cannot be identified as xml from the MIME type (or the extension). This can be a problem if you cannot set the mime type returned form the server (e.g. is someone elses’ server) and the extension is not .xml (in my case it is .opml).
A search on google didn’t come up with a solution, so here it is how i did it:

function parse(text) {
	var doc;
	if (typeof DOMParser != 'undefined') {
		var parser = new DOMParser();
		doc = parser.parseFromString(text, "text/xml");
	}
	else if (typeof ActiveXObject != 'undefined') {
		doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(text);
	}
	return doc;
}
function load (url, callback) {
	var httpRequest;
	if (typeof XMLHttpRequest != 'undefined') {
		httpRequest = new XMLHttpRequest();
	}
	else if (typeof ActiveXObject != 'undefined') {
		httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
	}
	if (httpRequest) {
		httpRequest.open('GET', url, true);
		httpRequest.onreadystatechange = function () {
			if (httpRequest.readyState == 4 &&
				httpRequest.status == 200) {
				callback(httpRequest.responseText);

			}
		};
		httpRequest.send(null);
	}
}

I did separate the loading and parsing phases, and using DOMParser for Mozilla/Firefox and XMLDOM for IE, passing them the xmlText. This has been tested with Firefox and InternetExplorer 6