...

View Full Version : Changing properties added with the Prototype keyword



johnnyb
05-02-2007, 10:39 PM
Hi,

I have some script like this:



// simplified to remove all the try/catches & stuff
function newXHR() {
request = new XMLHttpRequest();
XMLHttpRequest.prototype.requested = null;
return request;
}


then later:


function doRequest(url, callback) {
var req = newXHR();
req.requested = url;

// et cetera....
}


This works fine in Firefox, however the line 'req.requested = url'; causes an error in IE7, (I haven't tested IE6 yet). The error is 'Object doesn't support this property or method'

So, this leads me to believe that either a) XMLHttpRequest.requested is not writable, or, b) XMLHttpRequest.requested was never created in the first place. I think it's the first option because I'm not getting an error in the newXHR() function.

I've been searching for some documentation on this but I can't seem to find anywhere if properties created with the prototype keyword are writeable in IE. Does anyone know what the problem is?

Thanks,

John

david_kw
05-02-2007, 11:14 PM
In IE6 the XMLHttpRequest is an ActiveX object, not a native object so you can't set any custom properties on it.

IE7 on the other hand should support it.

http://msdn2.microsoft.com/en-us/library/ms537505.aspx

Plus I did this quick test on a straight custom property and it worked



<html>
<head>
<title>Junque</title>
<script type="text/javascript">
function doIt(s) {
var adiv = document.getElementById("adiv");
var xhr = new XMLHttpRequest();
xhr.custom_prop = "hiya";
adiv.innerHTML = xhr.custom_prop;
}
</script>
</head>
<body>
<div>
<button onclick="doIt();">Do it</button>
<br />
<div id="adiv"> </div>
</div>
</body>
</html>


But the question was about prototype and from what I read the "native" xhr for IE7 is still not truly native so you can't add to its prototype. Nor can you add to the XMLHttpRequest object itself, so only its instantiated objects.

That's my brain dump on it anyway. ;)

david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum