473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

xmlhttprequest question

I have been experimenting with xmlhttprequest. (I am a javascript
newbie, but familiar with programming with VB, Perl, and some PHP.) I
am trying to see if it's possible to receive html snippets back from a
request, and then insert that snippet into the target page; for example,
getting back a set of <ul> lists within lists, or a set of <option>
tags, which could then be added to a page's extent lists, or to select
inputs.

I'm trying to do this by setting the content-type header in the returned
data to text/xml, and I'm able to insert the nodes into the target page;
but then they will not be formatted with any css page I may have set
within the target page. Taking the aforementioned <ul> list, if the
target page references a css page with some layout rules for ul, the
inserted <ul> nodes aren't formatted using these rules.

Here's my code:

if (xmlhttp) {
xmlhttp.open("GET", "[command]",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var insertHere = document.getElementById('writeroot');
var respDoc = xmlhttp.responseXML;
insertHere.parentNode.insertBefore(respDoc.childNo des
[0].cloneNode(true),insertHere);
}
}
}

If anyone has any suggestions for a different way to do this, I'd be
very grateful. (I'm trying to do it this simply, that is, just insert
nodes from a request result into the target page. My hope is to avoid
writing extra list-parsing logic on the client side of my app.)

Jul 23 '05 #1
3 1302


spark wrote:
I have been experimenting with xmlhttprequest. (I am a javascript
newbie, but familiar with programming with VB, Perl, and some PHP.) I
am trying to see if it's possible to receive html snippets back from a
request, and then insert that snippet into the target page; for example,
getting back a set of <ul> lists within lists, or a set of <option>
tags, which could then be added to a page's extent lists, or to select
inputs.

I'm trying to do this by setting the content-type header in the returned
data to text/xml, and I'm able to insert the nodes into the target page;
but then they will not be formatted with any css page I may have set
within the target page. Taking the aforementioned <ul> list, if the
target page references a css page with some layout rules for ul, the
inserted <ul> nodes aren't formatted using these rules.
Make sure you send elements in the XHTML namespace e.g.
<ul xmlns="http://www.w3.org/1999/xhtml">
<li>Kibo</li>
</ul>
then with Mozilla you can do:

if (xmlhttp) {
xmlhttp.open("GET", "[command]",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var insertHere = document.getElementById('writeroot');
var respDoc = xmlhttp.responseXML;
insertHere.parentNode.insertBefore(respDoc.childNo des
[0].cloneNode(true),insertHere);


insertHere.parentNode.insertBefore(
insertHere.ownerDocument.importNode(respDoc.docume ntElement, true),
insertHere
);

That should then also work with other browsers having a common DOM
implementation for HTML and XML but with IE/Win that approach is not
possible, IE doesn't have an XML DOM implementation but uses MSXML to
parse XML and you can't move nodes from the MSXML XML DOM implementation
to the IE HTML DOM implementation. So with IE you are left to using
responseText and insert that e.g.
someElement.innerHTML = xmlhttp.responseText;
or
someElement.insertAdjacentHTML('beforeEnd', xmlhttp.responseText);
or you would need to write importNode for IE e.g. walk the MSXML XML DOM
and create all nodes as needed in the IE HTML DOM. But for instance the
Sarissa project suggests that IE with innerHTML performs better than an
attempt to implement importNode with DOM createXXX method calls so there
importNode is "implemented" by creating a div, setting its innerHTML and
then using the child nodes.

The Sarissa project is here:
<http://sarissa.sourceforge.net/>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
In article <42***********************@newsread2.arcor-online.net>,
ma*******@yahoo.de says...

Make sure you send elements in the XHTML namespace e.g.
<ul xmlns="http://www.w3.org/1999/xhtml">
<li>Kibo</li>
</ul>
then with Mozilla you can do:

[example code snipped]
That should then also work with other browsers having a common DOM
implementation for HTML and XML but with IE/Win that approach is not
possible, IE doesn't have an XML DOM implementation but uses MSXML to
parse XML and you can't move nodes from the MSXML XML DOM implementation
to the IE HTML DOM implementation.


Thanks Martin!!

Actually I'm not too concerned (at the moment at least) about IE, as the
app I'm writing will not be available on a public webpage, and so I'm
focusing my development on firefox only.

I have an additional question. I put the namespace declaration into my
returned doc, and now the ul list, and the ul lists within it, are all
formatted as html. However, this particular ul list was set up in my
css sheet as _drop down menus_ - in other words, the top (or root) ul
menu is displayed horizontally, and all ul list below the top are
invisible ( display: none; ) until the user hovers the cursor over an
item in the top list; and a few other effects. And this additional css
stuff is missed entirely. So it appears that the namespace has some
basic formatting commands, but not others. I tried placing a reference
to my stylesheet into the returned xml doc ( <?xml-stylesheet .. ?> ),
but that doesn't seem to work. Are there different namespaces I can
include that might give me the desired formatting? Is there a webpage
where I can view available public namespaces? Or is there a better way
to refer to my stylesheet? Or would I have to recreate my stylesheet as
an XML schema of some kind (I see on http://www.w3.org/TR/REC-xml-names/
that sometimes a namespace is declared via reference to a schema?)?

Thanks again!
Jul 23 '05 #3
In article <MP***********************@news-central.giganews.com>,
no@no.no says...
In article <42***********************@newsread2.arcor-online.net>,
ma*******@yahoo.de says...

Make sure you send elements in the XHTML namespace e.g.
<ul xmlns="http://www.w3.org/1999/xhtml">
<li>Kibo</li>
</ul>
then with Mozilla you can do:

[example code snipped]

That should then also work with other browsers having a common DOM
implementation for HTML and XML but with IE/Win that approach is not
possible, IE doesn't have an XML DOM implementation but uses MSXML to
parse XML and you can't move nodes from the MSXML XML DOM implementation
to the IE HTML DOM implementation.

I have an additional question.

[snip] So it appears that the namespace has some
basic formatting commands, but not others.


You know what, I goofed. I had no reference in my experimental document
to the css sheet I was talking about. Once I put that reference in, the
inserted xml/html doc was formatted correctly.

So - thank again Martin!
Jul 23 '05 #4

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

Similar topics

1
by: Elizabeth Harmon | last post by:
Good Evening, I am new to the XMLHTTPRequest Object and it's use but i am doing pretty good with it since it's realtively simple? I am having one problem in using a Get method. I am...
20
by: chris.schwalm | last post by:
This is part II of this <a...
1
by: 4levels | last post by:
Dear Folks, I stumbled upon a strange behaviour of the XMLHttpRequest.. Maybe I'm just not well informed enough about its possibilities, so could someone please confirm my question? When I...
7
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it...
1
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result...
1
by: vocalise | last post by:
The title probably isn't very clear, but I haven't been able to find this problem (or I must be having problems figuring out which search strings to use) so I apologize if this has been addressed...
5
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
5
by: HugeBob | last post by:
Hi All, I've got a question about Asynchronous vs Synchronous mode with the XMLHttpRequest object. What are the ramifications of using one mode vs the other? If the script uses Asynchronous...
25
by: q-rious | last post by:
Hello All, I have a question regarding XMLHttpRequest. I have the following code (as part of a much larger file): function loadXMLDoc(url) { // branch for native XMLHttpRequest object if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.