[Greasemonkey] XMLHttpRequest and document.domain

Lenny Domnitser ldrhcp at gmail.com
Mon Jul 25 00:53:04 EDT 2005


Apparently, document.domain can be changed to a lower level domain,
but not higher level. From my testing on http://www.google.com/ with
the JS shell [1], these work:

document.domain = 'www.google.com';
document.domain = 'google.com';
document.domain = 'com';

These do not work:

document.domain = 'foo.google.com';
document.domain = 'foo.www.google.com';
document.domain = 'yahoo.com';
document.domain = '';

I do not know why changing document.domain is allowed or useful, but
the Times of India website has this code:

function strrev(str) { if (!str) return ''; 
var revstr='';
for (i = str.length-1; i>=0; i--)
revstr+=str.charAt(i);return revstr;
}var sdomain = strrev(document.domain);
try{document.domain =
strrev(sdomain.substring(0,sdomain.indexOf('.',[(sdomain.substring(0,sdomain.indexOf('.')+1)).length])));}
catch(er){}

As far as I can see, there is no way to use normal XMLHttpRequest
under these conditions.

[1]: http://www.squarefree.com/bookmarklets/webdevel.html#shell


More information about the Greasemonkey mailing list