473,396 Members | 1,703 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.

simplifying appendChild for multiple elements

I have a rather old script that uses XMLHttpRequest to retrieve some
html and pops it into a div using innerHTML. I'd like to switch over to
using append child since not only is it standardized, but it'll save me
bandwidth in the long run.

The html returned looks something like:
----
<div style="position:absolute; z-index:50; width:256px; height:256px;
top:0px; left:0px">'.$tilesOverlay[NW].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:0px; left:256px">'.$tilesOverlay[NC].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:0px; left:512px">'.$tilesOverlay[NE].'</div>

<div style="position:absolute; z-index:50; width:256px;
height:256px; top:256px; left:0px">'.$tilesOverlay[W].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:256px; left:256px">'.$tilesOverlay[C].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:256px; left:512px">'.$tilesOverlay[E].'</div>

<div style="position:absolute; z-index:50; width:256px;
height:256px; top:512px; left:0px">'.$tilesOverlay[SW].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:512px; left:256px">'.$tilesOverlay[SC].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:512px; left:512px">'.$tilesOverlay[SE].'</div>
---

with obvious php elements in there.

as there's a total of nine divs, creating each of them and setting
their properties in a little loop shouldn't be a problem, but once I
start thinking of sending the remaining data over as a string and then
parsing through it as an array within the javascript, I'm struck there
must be an easier way. particularly since the z-index will change,
depending on what's calling that function.

Am I just being lazy, or am I missing something?

Oct 12 '06 #1
2 6997
suggestions for improvement welcome. to explain what this all does,
it's a set of images tiled (think google maps) that recieves lives
updates to its display via setInterval that sends an ajax request.
(note: RetrieveXML is the contents of an ajax request that returns a
string with php-generated variables. the format sent is the z-index
desired, a comma, then 9 pairs of reference IDs, a separating pipe,
the image sources for that tile, followed finally by a name to prepend
as the ID of the containing div)
cont = document.getElementById('eventContainer');
var len = cont.childNodes.length;
while (cont.hasChildNodes()) {
cont.removeChild(cont.firstChild);
}
var displayArray = RetrieveXML.split(',');
var positionArray = new Array(9);
positionArray[1] = 'top:0px; left:0px;';
positionArray[2] = 'top:0px; left:256px;';
positionArray[3] = 'top:0px; left:512px;';
positionArray[4] = 'top:256px; left:0px;';
positionArray[5] = 'top:256px; left:256px;';
positionArray[6] = 'top:256px; left:512px;';
positionArray[7] = 'top:512px; left:0px;';
positionArray[8] = 'top:512px; left:256px;';
positionArray[9] = 'top:512px; left:512px;';

for (var i = 1; i <= 9; i++) {
ts = document.createElement("DIV");
ts.setAttribute('style','position:absolute;
z-index:'+displayArray[0]+'; width:256px; height:256px;' +
positionArray[i]);
ts.setAttribute('ID',displayArray[10]+i);

document.getElementById('eventContainer').appendCh ild(ts);

tsi = document.createElement("IMG");
displayImage = displayArray[i].split('|');

if(displayImage[1] == null) { } else {
tsi.setAttribute('border','0');
tsi.setAttribute('alt',displayImage[0]);
tsi.setAttribute('title',displayImage[0]);
tsi.setAttribute('src',displayImage[1]);

document.getElementById(displayArray[10]+i).appendChild(tsi);
}
}
ad**@awayfromkeyboard.com wrote:
I have a rather old script that uses XMLHttpRequest to retrieve some
html and pops it into a div using innerHTML. I'd like to switch over to
using append child since not only is it standardized, but it'll save me
bandwidth in the long run.
(snip)

Oct 12 '06 #2
ad**@awayfromkeyboard.com wrote:
I have a rather old script that uses XMLHttpRequest to retrieve some
html and pops it into a div using innerHTML. I'd like to switch over to
using append child since not only is it standardized, but it'll save me
bandwidth in the long run.

The html returned looks something like:
----
<div style="position:absolute; z-index:50; width:256px; height:256px;
top:0px; left:0px">'.$tilesOverlay[NW].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:0px; left:256px">'.$tilesOverlay[NC].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:0px; left:512px">'.$tilesOverlay[NE].'</div>

<div style="position:absolute; z-index:50; width:256px;
height:256px; top:256px; left:0px">'.$tilesOverlay[W].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:256px; left:256px">'.$tilesOverlay[C].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:256px; left:512px">'.$tilesOverlay[E].'</div>

<div style="position:absolute; z-index:50; width:256px;
height:256px; top:512px; left:0px">'.$tilesOverlay[SW].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:512px; left:256px">'.$tilesOverlay[SC].'</div>
<div style="position:absolute; z-index:50; width:256px;
height:256px; top:512px; left:512px">'.$tilesOverlay[SE].'</div>
---

with obvious php elements in there.

as there's a total of nine divs, creating each of them and setting
their properties in a little loop shouldn't be a problem, but once I
start thinking of sending the remaining data over as a string and then
parsing through it as an array within the javascript, I'm struck there
must be an easier way. particularly since the z-index will change,
depending on what's calling that function.

Am I just being lazy, or am I missing something?
I like to use responseXML and importNode (if supported) or a recursive
append (if importNode is unsupported - IE for example). Doing it this
way means you have to be careful with your response (i.e. it must be
valid XML) or this will not work.

Your child post says you don't necessarily need to pass HTML fragments,
but perhaps only data. If this is the case I would use JSON to return
anonymous objects with the data I want, and write a simple function that
uses DOM methods to display it appropriately. There is a C-extension to
PHP that is incredibly fast at JSON encoding, and an alternative
raw-PHP version of the same if you are unable to install extensions. It
makes JSON as easy as

header("Content-Type: text/plain");
echo json_encode($anything);

Objects, arrays, strings, numbers - you name it, json_encode will
properly pass it to your javascript with one line of code. See
http://www.json.org for more details.

Jeremy
Oct 12 '06 #3

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

Similar topics

3
by: Ray | last post by:
I have put together some pretty simple code for adding and removing elements from an XML file, but am having a problem with toxml writing out the correct format after I have called appendChild on a...
25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
1
by: Nicolas Van Lancker | last post by:
Hi folks; I have this webpage, allowing users to insert multiple records in one post into the database. Because I don't know the exact number of records they want to add, I created a little...
1
by: Ryan Stewart | last post by:
If you don't want to read this post because of its length, I understand. I've spent two and a half days on this problem and have a good deal of information to relate. And this is kind of a long...
3
by: Robi | last post by:
I have the following code: ############## var nHead=(document.getElementsByTagName)?document.getElementsByTagName("head").item(0):document.head; var nStyle=document.createElement("style"); //...
2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
1
by: Dmitry Kulinich | last post by:
var iframe = document.createElement("<IFRAME id='frame0' style='PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px' border='no' name='frame0'...
2
by: vsanjit | last post by:
Hi all, I've been trying to create a table dynamically upon the generation of en event using the appendChild method in Javascript. This seems to work fine in Firefox, but not in IE7. There's also...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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.