I am using this Ajax script below to make cross domain Ajax calls which works fine if i use alert(txt);, but when i change it to document.write(txt); to print the result it stops working and i am not sure why it is not working,
It might be something stupid but i just can’t figure it out
function xss_ajax(url) {
var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
script.setAttribute('id', 'script_id');
script_id = document.getElementById('script_id');
if(script_id){
document.getElementsByTagName('head')[0].removeChild(script_id);
}
// Insert <script> into DOM
document.getElementsByTagName('head')[0].appendChild(script);
}
function callback(data) {
var txt = '';
for(var key in data) {
txt += key + " " + data[key];
}
//alert(txt);
document.write(txt);
}
var url = "http://myserver.com";
I believe that in some browsers you cannot call document.write after the document is done loading…
Edit: This stuff I’m striking out certainly does not work. As Teemu points out, calling “open” clears out the document. Also, MDN says that calling .write will implicitly call .open if you did not call it yourself.
The other possibility is that the document is just not rendering because when you start writing you’ll have to close the stream. You could try (not really sure if this works):However, I would strongly recommend that you just use a more standard method for writing to the document such as appendChild or using a framework (e.g. jQuery):