473,587 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HTMLDocument object from xmlHttpRequest

Is there a standard way of getting the HTMLDocument object
representation of a remote page using Javascript? If I request an
HTML page, the xmlHttpRequest returns either text or an XMLDocument.
I can't figure out how to convert the former into an HTMLDocument
object and the latter doesn't seem to work if the page isn't valid
XHTML.

If that approach isn't going to work, I've envisioned hacks involving
loading a page in an invisible iframe instead of using xmlHttpRequest,
but didn't want to rehack the wheel.

Also, this is for a Firefox extension, so I really only need something
that will work with Firefox, although it would be cool to find out how
to do this generally.

Thanks!
Jeff

Mar 11 '07 #1
5 3194
http://www.quirksmode.org/dom/importxml.html to grab the XML nodes and their
content into the document markup by means of DOM by using the .responseXML, as
for inserting it from .responseText property, just use .innerHTML property, be
keen that .innerHTML can be have issues since it calls on the html parser to
parse the markup and sometimes when you try to access its childNodes in DOM,
they may go astray. If you do not need to crawl its childNodes,
then .innerHTML works fine.

Danny
Mar 11 '07 #2
On Mar 10, 7:03 pm, Danny <dann90...@blue bottle.comwrote :
http://www.quirksmode.org/dom/importxml.htmlto grab the XML nodes and their
content into the document markup by means of DOM by using the .responseXML, as
for inserting it from .responseText property, just use .innerHTML property, be
keen that .innerHTML can be have issues since it calls on the html parser to
parse the markup and sometimes when you try to access its childNodes in DOM,
they may go astray. If you do not need to crawl its childNodes,
then .innerHTML works fine.
Thanks for the suggestions.

I want to do the later, but it's not exactly straightforward from
there. First, I need to somehow create a HTMLDocument, then I'm
guessing insert an HTMLElement node, then remove the <htmland </
htmltags from the responseText, then set the innerHTML of the
HTMLElement to that text.

If I do that, will XPATH queries work on the resulting document? I'm
happy even if it only works on Firefox 2+.

Thanks,
Jeff
>
Danny

Mar 11 '07 #3
Jeff wrote:
On Mar 10, 7:03 pm, Danny <dann90...@blue bottle.comwrote :
>http://www.quirksmode.org/dom/importxml.htmlto grab the XML nodes and their
content into the document markup by means of DOM by using the .responseXML,
as for inserting it from .responseText property, just use .innerHTML
property, be keen that .innerHTML can be have issues since it calls on the
html parser to parse the markup and sometimes when you try to access its
childNodes in DOM,
they may go astray. If you do not need to crawl its childNodes,
then .innerHTML works fine.

Thanks for the suggestions.

I want to do the later, but it's not exactly straightforward from
there. First, I need to somehow create a HTMLDocument, then I'm
guessing insert an HTMLElement node, then remove the <htmland </
htmltags from the responseText, then set the innerHTML of the
HTMLElement to that text.

If I do that, will XPATH queries work on the resulting document? I'm
happy even if it only works on Firefox 2+.

Thanks,
Jeff
>>
Danny

mydiv=document. createElement(' div');
mymarkup=OBJ.re sponseText.repl ace(/<\/*(html|head|bod y)>/i,''); // what would
there be an <htmlin the fragment markup, it should just be markup to be
inserted in a page's body

mydiv.innerHTML =mymarkup;
document.body.a ppendChild(mydi v);
if atop of the page
document.body.I nsertBefore(myd iv, document.body.f irstChild);

Danny
Mar 11 '07 #4
On Mar 10, 7:28 pm, Danny <dann90...@blue bottle.comwrote :
Jeff wrote:
On Mar 10, 7:03 pm, Danny <dann90...@blue bottle.comwrote :
>http://www.quirksmode.org/dom/importxml.htmltograb the XML nodes and their
content into the document markup by means of DOM by using the .responseXML,
as for inserting it from .responseText property, just use .innerHTML
property, be keen that .innerHTML can be have issues since it calls on the
html parser to parse the markup and sometimes when you try to access its
childNodes in DOM,
they may go astray. If you do not need to crawl its childNodes,
then .innerHTML works fine.
Thanks for the suggestions.
I want to do the later, but it's not exactly straightforward from
there. First, I need to somehow create a HTMLDocument, then I'm
guessing insert an HTMLElement node, then remove the <htmland </
htmltags from the responseText, then set the innerHTML of the
HTMLElement to that text.
If I do that, will XPATH queries work on the resulting document? I'm
happy even if it only works on Firefox 2+.
Thanks,
Jeff
Danny

mydiv=document. createElement(' div');
mymarkup=OBJ.re sponseText.repl ace(/<\/*(html|head|bod y)>/i,''); // what would
there be an <htmlin the fragment markup, it should just be markup to be
inserted in a page's body

mydiv.innerHTML =mymarkup;
document.body.a ppendChild(mydi v);
if atop of the page
document.body.I nsertBefore(myd iv, document.body.f irstChild);
Well, for instance if I request a URL, say http://www.yahoo.com/ and
to easily select elements of its DOM (It's a Greasemonkey script, so
the cross-site restrictions don't apply). So, I need a way of
creating a HTMLDocument and then initializing it. I assume I can do
something similar to the code you've provided, but it won't be exactly
like that.

-Jeff

Mar 11 '07 #5
Jeff wrote:
If that approach isn't going to work, I've envisioned hacks involving
loading a page in an invisible iframe instead of using xmlHttpRequest,
but didn't want to rehack the wheel.

Also, this is for a Firefox extension, so I really only need something
that will work with Firefox, although it would be cool to find out how
to do this generally.
With Mozilla, you need an invisible frame to parse a text/html document
into a DOM HTMLDocument. There is so far no API to parse a complete
text/html document into a DOM tree.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 11 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6329
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it didn't work. I discovered that this seemed to work: HTMLDocument.prototype.write= my_doc_write ; Why does HTMLDocument work here but not...
1
8763
by: kavitha | last post by:
Can we construct a HTMLDocument? I mean something like this string outerHTML = "<HTML><BODY>Some sample text...</BODY></HTML>"; HTMLDocument doc = new HTMLDocument(); doc.addElement("HTML"); doc.all.item("HTML", 0).outerHTML = outerHTML; Actually I have a url, I need to build a HTMLDocument
8
8204
by: pierre | last post by:
Hi, I got a problem which may easy to resolve, but I can't find any issue: I want to parse html files, so, I want first get it from an url, and I do like that: Dim objMSHTML As New mshtml.HTMLDocument() Dim objDocument As mshtml.HTMLDocument objDocument = objMSHTML.createDocumentFromUrl("http://www.google.fr",
0
1955
by: Filippo Bettinaglio | last post by:
VS2005, C# I have developed a UserControl embedded in a HTML web page. And I can access to the DOM with the following code: HTML page: …….. <BODY onload=loadDoc()> …….
0
2685
by: Irfan | last post by:
Hello, I want to load HTML file into HTMLDocument object. I don't want to use webbrowser object or any asyncrohonous call to load HTML into this file. Like if I call HTTPWebRequest to download the web page, I can somehow redirect the received HTML in HTMLDocument object. Any help how to do this. Thanks & Regards,
0
1458
by: forcedfx | last post by:
I'm faced with a bit of a conundrum. I'm trying to post a form using the HTMLDocument object. I've got the form posting working prefectly, however, in order to retrieve the HTML page that contains the form element I need to authenticate wih the server. Using Internet Explorer or FireFox when retrieving the HTML page I am presented with the...
2
2201
by: Jeff | last post by:
Hello, I assigned a new object to a local variable ("req") in a function (see below). The local variable "req" is obviously destroyed when the function exits, but should the object referenced by the variable live on? It seems that it does (and I want it to), but is this correct? I thought that local variables (and objects) are destroyed...
1
8365
nmm32
by: nmm32 | last post by:
I am trying to use Ajax with JSP to populate a 2nd drop down list based on the chosen option of the first one. I am getting the error "Object Expected" on the line 13-14 of the HTML: <script language="javascript"> // Global Variable for XmlHttp Request Object var xmlhttp; var yearDDL; function handleOnChangepType(ddl) {
0
1869
by: nickin4u | last post by:
I have a application that is used to automate certain task, I have been using mshtml.HTMLDocument class but certain events like click a button do not fire. I have tried a number of combinations but in vain. I was now trying to use System.Windows.Forms.Htmldocument class; here is the code Dim do1 As System.Windows.Forms.HtmlDocument =...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.