Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19935

Re: How to load common HTML code that include <Script>...</Script> to another HTML

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2479513.IYb1XUWMjp@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 24 May 2013 21:50:36 +0200
User-Agent KNode/4.10.2
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: How to load common HTML code that include <Script>...</Script> to another HTML
Newsgroups comp.lang.javascript
References <335e7844-2549-469d-a0e3-069005a5c697@v14g2000yqm.googlegroups.com> <eeb4dc48-e009-46a0-afbf-c03c70ac0884@googlegroups.com>
MIME-Version 1.0
Lines 129
NNTP-Posting-Date 24 May 2013 21:50:37 CEST
NNTP-Posting-Host dbfd8a23.newsspool3.arcor-online.net
X-Trace DXC=j]i4P_ji9_GlU`@c^jLCbJMcF=Q^Z^V3H4Fo<]lROoRA8kF<OcfhCOKe1h`^Sj[7oNDZm8W4\YJNLb@mF9jNikdEIQbgJMhLo0GU3\G:ZP\^QE
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:19935

Show key headers only | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to load common HTML code that include <Script>...</Script> to another HTML steve nospam <steve.nospamm@gmail.com> - 2013-05-23 22:26 -0700
  Re: How to load common HTML code that include <Script>...</Script> to another HTML steve nospam <steve.nospamm@gmail.com> - 2013-05-23 22:46 -0700
    Re: How to load common HTML code that include <Script>...</Script> to another HTML "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-24 09:41 +0200
  Re: How to load common HTML code that include <Script>...</Script> to another HTML Scott Sauyet <scott.sauyet@gmail.com> - 2013-05-24 08:24 -0700
    Re: How to load common HTML code that include <Script>...</Script> to another HTML Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-05-25 21:17 +0100
  Re: How to load common HTML code that include <Script>...</Script> to another HTML Danny <dann90038@gmail.com> - 2013-05-24 11:53 -0700
    Re: How to load common HTML code that include <Script>...</Script> to another HTML Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-24 21:50 +0200
      Re: How to load common HTML code that include <Script>...</Script> to another HTML Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-24 22:15 +0200
  Re: How to load common HTML code that include <Script>...</Script> to another HTML JJ <duh@nah.meh> - 2013-05-25 04:16 +0700

csiph-web