[TAG] Talkback:123/smith.html

Jimmy O'Regan joregan at gmail.com
Tue Jun 3 02:11:51 MSD 2008


I was trying to remember how to use XMLHttpRequest, and just wanted to
pop a note to say thanks for a useful article!

One thing: if you're trying to test something offline, Firefox will
disallow any open() requests (to try to prevent XSS).
http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php
has some details about it.

Or, better yet, here's the test page I was using (the rest of 'base'
is 'webservice/ws.php' - I don't think Apertium's server is quite up
to widespread use of the webservice yet):

```
<html>
<head>
<title>Translate as you type</title>

<script language=javascript type="text/javascript">
<!--
var base = "http://xixona.dlsi.ua.es/"

var http_request;
function GetTranslation(dir, data) {

	// For offline testing:
	// (see: http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php)
	//try {
	//	netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	//} catch (e) {
	//	alert("Permission UniversalBrowserRead denied.");
	//}

	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		http_request = new ActiveXObject ("Microsoft.XMLHTTP");
	}

	if (!http_request) {
		alert ("Cannot create XMLHttpRequest instance");
		return false;
	} else {
		var url = base + "?mode=" + dir + "&text=" + escape(data);
		//alert(url);
		http_request.abort();
		http_request.onreadystatechange = GetAsync;
		http_request.open("GET", url, true);
		http_request.send(null);
	}
}

function GetAsync() {
	if (http_request.readyState != 4 || http_request.status != 200) {
		return;
	}

	document.getElementById("translation").innerHTML=
		http_request.responseText;
	//alert(http_request.responseText);
	setTimeout("GetAsync()", 100);
	return;
}
// -->
</script>
</head>

<body>
<!-- direction hardcoded -->
<textarea id="input" onkeyup='GetTranslation("en-es",
document.getElementById("input").value)'>
</textarea>

<p id="translation"></p>

</body>
</html>
'''




More information about the TAG mailing list