Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #39027
| Path | csiph.com!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.33.237.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: check is referenced element still exists |
| Date | Thu, 22 Oct 2020 03:59:04 +0200 |
| Organization | PointedEars Software (PES) |
| Lines | 74 |
| Message-ID | <9780698.nUPlyArG6x@PointedEars.de> (permalink) |
| References | <bqudnUDYr6wRRA3CnZ2dnUU7-LXNnZ2d@westnet.com.au> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| Injection-Info | gwaiyur.mb-net.net; posting-host="33.237.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.237.33"; logging-data="1332"; mail-complaints-to="abuse@open-news-network.org" |
| User-Agent | KNode/4.14.10 |
| Cancel-Lock | sha1:uSGVfP76OXlw5pLsVjCmiY91YIo= |
| 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 |
| X-User-ID | U2FsdGVkX19n+x5GqolIOiv0h8s+8RsT+eSjeX0DeLLu6aOoGlJnFQ== |
| 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== |
| Xref | csiph.com comp.lang.javascript:39027 |
Show key headers only | View raw
Andrew Poulos wrote:
> If I have this
>
> let bar;
>
> if (document.querySelector('#foo')) {
> bar = document.querySelector('#foo');
> }
let bar = document.getElementById('foo');
is equivalent to the above, more efficient, and backwards-compatible on the
right-hand side. For maximum possible backwards-compatibility, consider
var bar = document.getElementById('foo');
and potential wrapper methods.
> and at some stage this happens
>
> bar.remove();
Note that ChildNode::remove(), whose implementation is used there, is
specified in the WHATWG DOM only and is not supported by Internet Explorer
and older HTML user agents. Use
bar.parentNode.removeChild(bar);
in backwards-compatible code instead. A wrapper method of the form
dom.removeChild = function (childNode) {
if (dom.isHostMethod(childNode, 'remove'))
{
childNode.remove();
}
else
{
childNode.parentNode.removeChild(childNode);
}
};
may be called instead, and calls to it can be easily replaced (by methods
such as full-text search-and-replace) by .remove() calls once backwards
compatibility is no longer considered important.
> Without directly knowing the element's original ID is the only way to
> know whether the referenced element still exists is by doing something
> like
>
> if (document.querySelector(bar.id)) {
> //...
> }
>
> ?
No.
The “parentNode” attribute of Nodes that are not part of a document tree
(has not been inserted yet or has been removed already) is specified to be
“null” (a OMG/Web IDL special value); therefore, the “parentNode” property
of an element object that is not part of a document tree is the “null” value
(of the Null type) in ECMAScript implementations.
<https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-1950641247>
and, more cryptically,
<https://dom.spec.whatwg.org/#dom-node-parentnode>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
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
check is referenced element still exists Andrew Poulos <ap_prog@hotmail.com> - 2020-10-22 11:59 +1100
Re: check is referenced element still exists Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-10-22 03:59 +0200
Re: check is referenced element still exists JJ <jj4public@gmail.com> - 2020-10-22 14:22 +0700
Re: check is referenced element still exists Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-10-22 14:14 +0200
Re: check is referenced element still exists "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-10-22 11:35 -0700
Re: check is referenced element still exists Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-10-23 21:35 +0200
csiph-web