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


Groups > comp.lang.javascript > #39027

Re: check is referenced element still exists

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: check is referenced element still exists
Date 2020-10-22 03:59 +0200
Organization PointedEars Software (PES)
Message-ID <9780698.nUPlyArG6x@PointedEars.de> (permalink)
References <bqudnUDYr6wRRA3CnZ2dnUU7-LXNnZ2d@westnet.com.au>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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