Jump to content

reading html with ajax


para

Recommended Posts

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 point

the 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 something

why doesn't it print out the html?
Link to comment
https://forums.phpfreaks.com/topic/34991-reading-html-with-ajax/
Share on other sites

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?
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.