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


Groups > comp.lang.javascript > #39026 > unrolled thread

check is referenced element still exists

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2020-10-22 11:59 +1100
Last post2020-10-23 21:35 +0200
Articles 6 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#39026 — check is referenced element still exists

FromAndrew Poulos <ap_prog@hotmail.com>
Date2020-10-22 11:59 +1100
Subjectcheck is referenced element still exists
Message-ID<bqudnUDYr6wRRA3CnZ2dnUU7-LXNnZ2d@westnet.com.au>
If I have this

let bar;

if (document.querySelector('#foo')) {
   bar = document.querySelector('#foo');
}

and at some stage this happens

bar.remove();


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)) {
   //...
}

?

Andrew Poulos

[toc] | [next] | [standalone]


#39027

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2020-10-22 03:59 +0200
Message-ID<9780698.nUPlyArG6x@PointedEars.de>
In reply to#39026
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.

[toc] | [prev] | [next] | [standalone]


#39028

FromJJ <jj4public@gmail.com>
Date2020-10-22 14:22 +0700
Message-ID<1fwp7byxju1hn$.ugackqir7itz$.dlg@40tude.net>
In reply to#39026
On Thu, 22 Oct 2020 11:59:53 +1100, Andrew Poulos wrote:
> If I have this
> 
> let bar;
> 
> if (document.querySelector('#foo')) {
>    bar = document.querySelector('#foo');
> }
> 
> and at some stage this happens
> 
> bar.remove();
> 
> 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)) {
>    //...
> }
> 
> ?
> 
> Andrew Poulos

Check the element's `parentNode` property. It'll be `null` if it's not
placed into the document. Be sure to check the property of the removable
element, and not any of its child element.

[toc] | [prev] | [next] | [standalone]


#39029

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2020-10-22 14:14 +0200
Message-ID<2119550.iZASKD2KPV@PointedEars.de>
In reply to#39028
JJ wrote:

> Check the element's `parentNode` property. It'll be `null` if it's not
> placed into the document. Be sure to check the property of the removable
> element, and not any of its child element.

… or, in general, child *nodes*, which includes text nodes.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#39032

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2020-10-22 11:35 -0700
Message-ID<75959220-d5e9-49fa-b8c7-3037966df23bn@googlegroups.com>
In reply to#39028
On Thursday, October 22, 2020 at 2:22:34 AM UTC-5, JJ wrote:

> Check the element's `parentNode` property. It'll be `null` if it's not 
> placed into the document. Be sure to check the property of the removable 
> element, and not any of its child element.

node.isConnected exists now:

<https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected>

[toc] | [prev] | [next] | [standalone]


#39035

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2020-10-23 21:35 +0200
Message-ID<1838941.PYKUYFuaPT@PointedEars.de>
In reply to#39032
Michael Haufe (TNO) wrote:

> On Thursday, October 22, 2020 at 2:22:34 AM UTC-5, JJ wrote:
>> Check the element's `parentNode` property. It'll be `null` if it's not
>> placed into the document. Be sure to check the property of the removable
>> element, and not any of its child element.
> 
> node.isConnected exists now:
> 
> <https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected>

Thanks.

-- 
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web