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
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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.