T
The Natural Philosopher
Jorge said:If the element has been inserted into the DOM by the html parser, then
yes, you need to find it first somehow, at least once, (walkTheDOM,
use a DOM search method (.getElementXXX) or lookup into an
htmlCollection) in order to get (and save) a reference to it.
If it was inserted into the DOM by your (JS) code, you already have a
reference to it (that you can save) so there's never the need to
search for it.
Ok. That is one pint finally resolved then.
You can build the contents of that shop's catalog at the server and
send it prebuilt tagged as html, or
You can build it dynamically, client-side, by sending the *data* from
the server, not markup no html (usually JSON).
Mmm. I think my ability to do that is constrained by my lack of
familiarity with javascript and the DOM so I'll fie hat for future
reference.
For example: the server's PHP can send it either:
prebuilt as html:
<div id="reference456">Chocolat Cookies</div>
<div id="reference457">Plum Cake</div>
Or as data:
<script>
var catalog= {
'reference456': 'Chocolat Cookies',
'reference457': 'Plum Cake'
};
</script>
Yup.
In the second case, the browser has the catalog as data, and the JS
code (client-side) has to build the presentation of that data:
<script>
(function () {
if (catalog && (typeof catalog === "object")) {
var itemRef, itemName, itemDOMElement;
for ( itemRef in catalog ) {
if (catalog.hasOwnProperty(itemRef)) {
//create the DOM element
itemDOMElement= document.createElement('div');
//Fill the div's content
itemDOMElement.innerHTML= itemName= catalog[itemRef];
//Pack it all into an object
catalog[itemRef]= {
'itemName': itemName,
'itemDOMElement': itemDOMElement,
'anotherUsefulProperty': Math.random()
};
//And insert the element into the page.
document.body.appendChild(itemDOMElement);
/* or:
document.body.appendChild(catalog[itemRef].itemDOMElement);
/*
}
}
}
})();
</script>
You'd end up with a nice JS data structure (object) : catalog= {
'reference456': {
'itemName': "Chocolat Cookies",
'itemDOMElement': (referenceToTheDivDOMElement),
'anotherUsefulProperty': (a random number)
},
'reference457': {
'itemName': "Plum Cake",
'itemDOMElement': (referenceToTheDivDOMElement),
'anotherUsefulProperty': (a random number)
}
}
...and no need to ever search the DOM. But, then, your page is turning
more and more into a webApp.
I don't really care about what its called, just that it actually works.
At the moment, the faster development route is to use html with some script.
Mostly because I am marginally more familiar with HTML than javascript.
I started out using form inputs and select boxes, but it was too
ugly..so the need to add more script has arrived.
I am not quite ready to go 'all script' as it were, but I take your
points to heart and to memory.
You have been *extremely* helpful, and I thank you for that.
I think what I have now works well enough across the browsers it will be
used with, and the cost benefit of going all script simply for
elegance's sake is not there.
It is in fact a webapp in the sense that its a series of forms into a
database, that simply happen to use a web browser as a means of access.
That turns out to be the simplest way to access the database over local
and remote links, on multiple platforms.
Its a million miles away from a public web site with 'scripted
decoration' , that's for sure.
Its been and continues to be an interesting exercise in code
optimisation: On the one hand it may well be used on relatively low
bandwidth connections, and there compressing the HTML has reduced page
load times impressively (well, if the machine that decompresses has the
horsepower anyway) and reducing visual lag by downloading a lot of the
data so that as much as possible apart from strict database updates, is
done in script in the browser.
I am not sure how the ZIP algorithm works, but it seems to be able to
turn 2000 lines of essentially almost identical information into not
very much at all.
Its now at the stage where flyouts follow the mouse with no perceptible
delays, which is all it needs to be. As opposed to the worst case last
week, where 1.5 seconds was the time.
My biggests lag locally is now decompressing the page..about half a second.
A also managed to turn about 100 nested SQL enquiries into a single
incredibly ugly, but a single, one. That , mysql informs me, takes a
mere 40ms to execute, and less if its cached..;-)
So all intersting stuff, and again, many many thanks for the help.