Euan Ritchie's playground

Navigation

Search this site

Login More quotes…

This is a demonstration of a simple method for making remote calls via javascript. It works in DOM compliant browsers and does not require any ActiveX, complex objects such as XMLHTTPRequest or gnarly arrangements of iframes or the like. It solely relies on simple widely supported javascript.

It also crosses domains without trouble. I expect this is a security hole that will be plugged in future.

Enter any characters

The code on this page

<script language="Javascript" src="remote.js"></script>
<script>
	function remoteCallBack(strParam) {
		document.getElementById('data').value = strParam;
	}
	
	function reverseData() {
		remoteCall("http://puna.net.nz/remote/remote.asp", document.getElementById('data').value);
	}
</script>

The code in remote.js

//Optional fix for firefox interface bug
function firefoxbug() {
	if (!window.firefox&&!document.all) { 
		window.firefox = document.createElement('iframe');
		firefox.style.display = 'none';
		document.getElementsByTagName('body').item(0).appendChild(firefox);
	}			
}

function firefoxClear() {
	if (window.firefox) {window.firefox.src = 'empty.js'}
}

function remoteCall(URL, param) {
	var head = document.getElementsByTagName('head').item(0);
	if (head) {
		firefoxbug();

		//construct querystring. 
		//	If more than one parameter (beyond URL) is supplied they are considered name/value pairs.
		//	Any odd parameter is considered the value for the 'param' parameter.
		var querystring = '';
		for (var i=1;i < remoteCall.arguments.length; i++) {
			if ((i+1) < remoteCall.arguments.length) {
				querystring += '&' + remoteCall.arguments[i] + '=';
				i++;
			} else {
				querystring += '&param=';
			}
			querystring += escape(remoteCall.arguments[i]);
		}

		//script block to append, append querystring of parameters
		var script = document.createElement('script');
		script.setAttribute('src', URL + '?time=' + new Date().toGMTString() + querystring);
		script.setAttribute('id', 'remote');
				
		//remove prior appended script (will not intefer, remove any javascript or change variables, but stops DOM from bloating)
		var prior = document.getElementById('remote');
		if (prior) {head.removeChild(prior)}
		
		//appending script executes it, the server will return script that calls remoteCallback(), so it's an asynchronous call.
		head.appendChild(script);
	}
}

The code in remote.asp

remoteCallback('<%

	dim input, i, output
	input = request("param")

	for i = 1 to len(input)
		output = mid(input, i, 1) & output
	next

	response.write replace(output, "'", "\'")

%>');

firefoxClear();