Danny wrote:
> to steve nospam
When will you ever learn to at least keep the attribution that your
otherwise completely borken Google Groups automatically inserts for you?
It inserts an attribution novel to be sure, but that is still better than
the nonsense that you post instead. Usenet is _not_ a one-to-one
communications medium. (And you do not really assume that “nospam” is
“steve”'s last name, do you?)
> the only more-or-less feasible solution, which I've used btw, is to use an
> XmlHttprequest() object, to fetch the "content html files" that you'd be
> dumping in your "template html" file, with all proper ID'ed block-level
> elements
>
> Bear in mind they do not have to be as .html mimetype,
“.html” is not a “mimetype”, it is a file/resource name suffix. The MIME
media type for HTML is “text/html”, see RFC 2854 and various HTML
Specifications.
> they can be .txt or whatever else, it just have to be valid markup, and
> also be aware of the issues when using the innerHTML property, innerHTML
> is great for WRITES, no-so-great for READS, as in when reading or
> accessing nodes added with it.
In English: The “innerHTML” property value, when read, is a serialization of
the document subtree associated with the corresponding element. That
serialization is not standardized, therefore not reliable as such. But it
is still compatible within the same user agent, that is, it is safe for
moving content there.
> As opposed to using DOM methods for adding elements/properties, which
> long-windy as they're, they're quite reliable for WRITE and READS
In English: Those methods operate directly on the document tree, and their
outcome is standardized per the W3C DOM Specifications.
> [pretty-printed]
> ----------------------------------------
> function fetcher(myfile, mydivID){
> var xhrObject = new XmlHttpRequest(),
There is no such method; the proper identifier is “XMLHttpRequest”. So the
buggy code that you posted does not even run.
> dest = document.getElementById(mydivID);
>
> xhrObject.open('get', myfile, true);
The HTTP verb SHOULD be uppercase.
> xhrObject.onreadystate = function () {
> if (xhrObject.readyState = 4) {
This assigns the value “4” to the “readyState” property, if possible (if
not, that is yet another runtime error). You were looking for a comparison
instead:
if (xhrObject.readyState == 4) {
However, this comparison is insufficient to determine if a response is
useful. It is necessary also to compare against the response status,
because the successfully received response might be an error response (such
as “404 File Not Found”):
if (xhrObject.readyState == 4
&& /\b(0|2\d\d|1223)\b/.test(xhrObject.status)
> dest.innerHTML = xhrObject.responseText;
It makes no sense to retrieve the target object before the response has been
received. Not only is there an unnecessary closure with a host object
(which leaks memory if you are not careful, and you *are* not) but also the
target element might be no longer be available when the response has been
received. Better:
var dest = document.getElementById(mydivID);
if (dest)
{
dest.innerHTML = xhrObject.responseText;
}
The closure makes sure that “mydivID” is available in the inner execution
context. “mydivID” does not store a reference to a host object, but holds a
string value, so there are no potential memory leaks.
BTW, all of this is old news. With JSX:http.js it would be more robust and
efficient in several ways:
var req = new jsx.net.http.HTTPRequest();
function fetcher (myfile, mydivID)
{
req.setURL(myfile);
req.setSuccessListener(function (response) {
var dest = document.getElementById(mydivID);
if (dest)
{
dest.innerHTML = response.responseText;
}
});
req.send();
}
> }
> }
> xhrObject.send(null);
> }
> ---------------------------------------
> then call your "fetcher" function like
>
> fetcher('header.html', 'myheader');
> fetcher('footer.html', 'myfooter');
And finally, your suggestion does not even solve the OP's problem: It will
*not* ”load common HTML code that include <Script>...</Script> to another
HTML” so that the code in the “script” elements or referred by them is
executed, because the “innerHTML” property does not work that way.
I think the proper expression is “fail”.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.