Request an HTML page via AJAX

J

J

Hi,

Ajax question: Can I retrieve an HTML page and use the DOM to grab
stuff from the requested page in a similar fashion to an XML page
requested in the same manner?

Details: I'm requesting a web page by:

myRequest.open("GET", "http://www.asite.com/page.html", true);

I can take the response and convert it to a string via:

myRequest.responseText.toString();

So that I can parse it to find a few things on the page that I am
looking for. If I were to retrieve an XML document, I can use the DOM
to grab stuff from the page with ease. Is there an AJAX-esque method
to retrieving an HTML page and using DOM to look for things on the
page? Converting an html page to a string and parsing it with
javascript string methods is hell.

Thanks everyone,
J
 
M

Martin Honnen

J said:
Details: I'm requesting a web page by:

myRequest.open("GET", "http://www.asite.com/page.html", true);

I can take the response and convert it to a string via:

myRequest.responseText.toString();

responseText gives you a string, why do you call toString() on that?
So that I can parse it to find a few things on the page that I am
looking for. If I were to retrieve an XML document, I can use the DOM
to grab stuff from the page with ease. Is there an AJAX-esque method
to retrieving an HTML page and using DOM to look for things on the
page?

If the document sent is well-formed XHTML then you can use responseXML.
So far there is no browser exposing its HTML parser the same way, if you
want to parse HTML documents you are better off loading them into an
iframe (that can be invisible).
 
J

J

Martin said:
responseText gives you a string, why do you call toString() on that?

Good question. I followed some examples that used it. I will drop the
toString() call.
If the document sent is well-formed XHTML then you can use responseXML.
So far there is no browser exposing its HTML parser the same way, if you
want to parse HTML documents you are better off loading them into an
iframe (that can be invisible).

Load page into an iframe. Sounds like a good idea. I will try that.
The types of pages I am looking at are not well-formed XHTML. Can I
still use responseXML to grab those pages and use DOM functions? I
think I tried this and had some trouble.
 
W

webEater

J said:
Good question. I followed some examples that used it. I will drop the
toString() call.


Load page into an iframe. Sounds like a good idea. I will try that.
The types of pages I am looking at are not well-formed XHTML. Can I
still use responseXML to grab those pages and use DOM functions? I
think I tried this and had some trouble.

Just use Iframes to emulate an XMLHttpRequest, if your browser does not
support it. E.g. IE 5/6 (without ActiveX) or Opera 7. Else you should
of course use a native request. To make a cross-domain request use a
php file that reads an external file via HTTP.

A framework that provides this functionality is UniAjax.
http://uniajax.net

Andi
 
R

Roy A.

webEater skrev:
Just use Iframes to emulate an XMLHttpRequest, if your browser does not
support it. E.g. IE 5/6 (without ActiveX) or Opera 7. Else you should
of course use a native request. To make a cross-domain request use a
php file that reads an external file via HTTP.

You shouldn't use XMLHttpReques if the document is pre-XHTML. It may
not work in browsers that is aware of the diffrence between SGML and
XML.
 
M

Martin Honnen

J said:
The types of pages I am looking at are not well-formed XHTML. Can I
still use responseXML to grab those pages and use DOM functions?

No, to populate responseXML the browser parses the HTTP response body as
XML and therefore will not build a DOM if the markup sent is not
well-formed.
 
R

Randy Webb

Roy A. said the following on 1/13/2007 7:27 AM:
webEater skrev:


You shouldn't use XMLHttpReques if the document is pre-XHTML.

Huh? That is nonsense if taken literally. If by "pre-XHTML" you are
referring to HTML4.01 or the likes then your statement is blatantly
false as IE doesn't have a clue what XHTML is and it works perfectly
well with an XMLHttpRequest when retrieving an HTML file.
 
R

Roy A.

Randy Webb skrev:
Roy A. said the following on 1/13/2007 7:27 AM:

Huh? That is nonsense if taken literally. If by "pre-XHTML" you are
referring to HTML4.01 or the likes then your statement is blatantly
false as IE doesn't have a clue what XHTML is and it works perfectly
well with an XMLHttpRequest when retrieving an HTML file.

Maybe you could use XMLHttpRequest to retrieve a sandwidch, but I
wouldn't call that AJAX.
 
R

Randy Webb

Roy A. said the following on 1/14/2007 3:12 PM:
Randy Webb skrev:

Maybe you could use XMLHttpRequest to retrieve a sandwidch, but I
wouldn't call that AJAX.

And maybe you can post to Usenet but that doesn't make what you say any
different or make it any less false. Your claim was with regards to
XHTML and whether "AJAX" works or not is irrelevant to whether it is
XHTML or HTML. If you want to apply a very strict definition of what is
or isn't AJAX then retrieving XHTML isn't "AJAX" either as the term was
originally coined to reflect retrieving XML from the server - not X/HTML.

So, what *would* you call "AJAX"?
 
R

Roy A.

Randy Webb skrev:
And maybe you can post to Usenet but that doesn't make what you say any
different or make it any less false. Your claim was with regards to
XHTML and whether "AJAX" works or not is irrelevant to whether it is
XHTML or HTML. If you want to apply a very strict definition of what is
or isn't AJAX then retrieving XHTML isn't "AJAX" either as the term was
originally coined to reflect retrieving XML from the server - not X/HTML.

Isn't XHTML an XML application?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top