Some Old, Broken JavaScript

KeyboardBack in the history times (10 or eleven years ago) the idea of using client-script to create living, breathing applications was still more dream than reality. It was possible, but very difficult. Moreover, anything you did get working might not work the next day due to the accelerated pace of browser development.

I had written a little script, a toy really, in an attempt to (innocently!) overcome a major feature of browser security: the domain sandbox. In short, this feature keeps script from any particular site from interfering with script from any other particular site. It would be bad, for example, if a site in one window could snatch the password from one in another window. The sandbox feature prevents this kind of cross-site scripting attack.

But what if the two sites were friendly? What if they wanted to share data? Because they were buddies? Huh? What then?!

Well, after pondering this “problem” for way too long I came up with a truly, revolutionarily stupid solution: I’d pass the data across the humble status bar! The sad thing is: it worked perfectly. It worked, in fact, for a long time and across every major browser.

Here’s a working example of the code I used (link will open a new tab/window). It creates three frames, two from this server and a third from another domain. Type something (like “top”, “middle” and “bottom”) into each of the small fields and hit “send”. Then press “Dump MQs!” to see the information that each has collected.

I’m happy to say that this code no longer allows for cross-domain data exchange in any modern browser I’ve tried. In FireFox (version 38) and Chrome (version 43) you’ll still be able to send data within a single domain (the first two windows in the example) but even this doesn’t function any longer in Internet Explorer 11.

It’s not useful – really, it never was –  but I still enjoy looking back on this kind of thing.

I’ve appended the core code, I called it “commune”, below for the curious (but be kind, remember that this is over a decade old).

var MQs = new Object();

var mPrefix = "}*{";
var mDelim = "|";

var rDelay = 25;
var wDelay = 50;
var wTries = 20;

	// Set browser class
IE = true;
if (navigator.appName.indexOf('Netscape') != -1) {
	IE = false;
};

function GetDateSeed() {
	var DateSeed = new Date();
	DateSeed = DateSeed.valueOf();
	return DateSeed;
};

function WriteStatus(Message, cnt) {
	if (!cnt) {
		cnt = 0;
	};
	if (cnt < wTries) {
		if (IE) {
			window.status = Message;
		} else {
			top.status = Message;
		};
		WriteStatusTimeOut = setTimeout("WriteStatus('" + Message + "', " + (cnt + 1) + ")", wDelay);
	};
};

function ReadStatus() {
	if (IE) {
		return window.status;
	} else {
		return top.status;
	};
};

function MessageQueue(Name, DateSeed) {
	this.uID = DateSeed;
	this.Name = Name;
	this.Messages = new Array();
	MQs[Name] = this;
};

function AddMessage(mQueueName, mText, mID) {
	if (!mID) {
		mID = MQs[mQueueName].Messages.length;
	};
	MQs[mQueueName].Messages[mID] = mText;
	return mID;
};

function PostMessage(mQueueName, mText, mID) {
	mID = AddMessage(MQs[mQueueName].Name, mText, mID);
	var cnt = 0;
	Message = mPrefix + mDelim + MQs[mQueueName].uID + mDelim + MQs[mQueueName].Name + mDelim + mID + mDelim + MQs[mQueueName].Messages[mID];
	WriteStatus(Message);
};

function ReadMessage(myQueueName) {
	if (ReadStatus()) {
		CurMessage = ReadStatus().split('|');
		if ((CurMessage[0] == mPrefix) && (CurMessage.length == 5) && (CurMessage[2] != myQueueName)) {
			if (window.MQs[CurMessage[2]]) {
				CurQ = window.MQs[CurMessage[2]];
			} else {
				CurQ = new MessageQueue(CurMessage[2], CurMessage[1]);
			};
			if (CurQ.uID != CurMessage[1]) {
				CurQ = new MessageQueue(CurMessage[2], CurMessage[1]);
			};
			AddMessage(CurMessage[2], CurMessage[4], CurMessage[3]);
		};
	};
	ReadTimeOut = setTimeout("ReadMessage('" + myQueueName + "')", rDelay);
};

function DumpMQs() {
	var out = "";
	for (Qs in MQs) {
		out = out + Qs + "\n";
		for (i = 0; i < MQs[Qs].Messages.length; i++) {
			out = out + "  - (" + i + ") " + MQs[Qs].Messages[i] + "\n";
		};
	};
	return out;
};

Leave a Reply