para Posted January 20, 2007 Share Posted January 20, 2007 Hello I am trying to make a script that can paste a part of a html with ajax. I thought this had to be the simplest thing one can do with ajax but now it seams like it might not work at all.[code] var XMLHttpRequestObject = false; var newhtml = null; if (window.XMLHttpRequest) { alert("nonactiveX"); XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { alert("activeX"); XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } if(XMLHttpRequestObject) { alert("sunt aici"); var theURL = "http://localhost/ajax.htm"; alert(theURL); XMLHttpRequestObject.open("GET", theURL); XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { alert("sunt aici 1"); newhtml = XMLHttpRequestObject.responseText; document.write(newhtml); } } XMLHttpRequestObject.send(null); }[/code]I want to get a subsitring from the variable newhtml like substring(newhtml,0,whatever) and just load a part of a file well actually the finished script should search newhtml for some tokens but that is besides the pointthe point is this doesn't work nothing gets written and if I use responseXML instead of responseText i get a blank page printed as if newhtml was empty or somethingwhy doesn't it print out the html? Quote Link to comment Share on other sites More sharing options...
para Posted January 20, 2007 Author Share Posted January 20, 2007 I did it like this:[code]var x = new ActiveXObject("Microsoft.XMLHTTP");var currentURL = window.location.href;alert(currentURL);x.open("GET",currentURL,true);x.send();x.onreadystatechange = function(){ if (x.readyState == 4 && x.status == 200) { var pm = x.responseText; alert(pm); document.write("something"+pm); alert("terminai"); }} [/code]this way I always see a messagebox with the page html but only once did it actually write it( the output was "somethig" and the the html)I thouht maby the page didn't have time to load so I moved the code into a function and I set it to start on load but no effect I also put the function in a timer but also nothing new. Whtat could be the cause of this? why doesn't this work every time? Quote Link to comment Share on other sites More sharing options...
BugX Posted January 26, 2007 Share Posted January 26, 2007 try this one... I think it'll work ur code is correct, but document.write sometimes raises a problem (I think)[quote]<div id ="test"></div><script> var XMLHttpRequestObject = false; var newhtml = null; if (window.XMLHttpRequest) { alert("nonactiveX"); XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { alert("activeX"); XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } if(XMLHttpRequestObject) { alert("sunt aici"); var theURL = "http://www.phpfreaks.com/forums/index.php/topic,123260.0.html"; alert(theURL); XMLHttpRequestObject.open("GET", theURL); XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { alert("sunt aici 1"); newhtml = XMLHttpRequestObject.responseText; document.getElementById("test").innerHTML = newhtml; } } XMLHttpRequestObject.send(null); }</script>[/quote]Regards,Andre Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.