Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1038134 of Node.isSameNode()

  • Revision slug: Web/API/Node/isSameNode
  • Revision title: Node.isSameNode()
  • Revision id: 1038134
  • Created:
  • Creator: jsx
  • Is current revision? Yes
  • Comment r+

Revision Content

{{APIRef("DOM")}}

The Node.isSameNode() method tests whether two nodes are the same, that is if they reference the same object.

Syntax

var isSameNode = node.isSameNode(other);

Parameters

  • other The {{domxref("Node")}} to test against.

Example

In this example, we create three {{HTMLElement("div")}} blocks. The first and third have the same contents and attributes, while the second is different. Then we run some JavaScript to compare the nodes using isSameNode() and output the results.

HTML

<div>This is the first element.</div>
<div>This is the second element.</div>
<div>This is the first element.</div>

<p id="output"></p>

JavaScript

let output = document.getElementById("output");
let divList  = document.getElementsByTagName("div");

output.innerHTML += "div 0 same as div 0: " + divList[0].isSameNode(divList[0]) + "<br/>";
output.innerHTML += "div 0 same as div 1: " + divList[0].isSameNode(divList[1]) + "<br/>";
output.innerHTML += "div 0 same as div 2: " + divList[0].isSameNode(divList[2]) + "<br/>";

Results

{{ EmbedLiveSample('Example', 480) }}

Specifications

Specification Status Comment
{{SpecName('DOM WHATWG', '#interface-node', 'Node')}} {{Spec2('DOM WHATWG')}} No change (was for a long time removed from it).
{{SpecName('DOM3 Core', 'core.html#Node3-isSameNode', 'Node.isSameNode()')}} {{Spec2('DOM3 Core')}} Initial definition

 

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatUnknown}} {{CompatGeckoDesktop("1.0")}}
Removed in {{CompatGeckoDesktop("10")}}
Readded in {{CompatGeckoDesktop("48")}}
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatGeckoMobile("1.0")}}
Removed in {{CompatGeckoMobile("10")}}
Readded in {{CompatGeckoMobile("48")}}
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

  • {{domxref("Node.isEqualNode()")}}

Revision Source

<div>{{APIRef("DOM")}}</div>

<p>The <code><strong>Node.isSameNode()</strong></code>&nbsp;method tests whether two nodes are the same, that is if they reference the same object.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
var <em>isSameNode</em> = <em>node</em>.isSameNode(<em>other</em>);
</pre>

<h3 id="Parameters">Parameters</h3>

<ul>
 <li><code>other</code> The {{domxref("Node")}} to test against.</li>
</ul>

<h2 id="Example">Example</h2>

<p>In this example, we create three {{HTMLElement("div")}} blocks. The first and third have the same contents and attributes, while the second is different. Then we run some JavaScript to compare the nodes using <code>isSameNode()</code> and output the results.</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">
&lt;div&gt;This is the first element.&lt;/div&gt;
&lt;div&gt;This is the second element.&lt;/div&gt;
&lt;div&gt;This is the first element.&lt;/div&gt;

&lt;p id="output"&gt;&lt;/p&gt;</pre>

<div class="hidden">
<h3 id="CSS">CSS</h3>

<pre class="brush: css">
#output {
&nbsp; width: 440px;
&nbsp; border: 2px solid black;
&nbsp; border-radius: 5px;
&nbsp; padding: 10px;
&nbsp; margin-top: 20px;
&nbsp; display: block;
}</pre>
</div>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">
let output = document.getElementById("output");
let divList  = document.getElementsByTagName("div");

output.innerHTML += "div 0 same as div 0: " + divList[0].isSameNode(divList[0]) + "&lt;br/&gt;";
output.innerHTML += "div 0 same as div 1: " + divList[0].isSameNode(divList[1]) + "&lt;br/&gt;";
output.innerHTML += "div 0 same as div 2: " + divList[0].isSameNode(divList[2]) + "&lt;br/&gt;";
</pre>

<h3 id="Results">Results</h3>

<p>{{ EmbedLiveSample('Example', 480) }}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#interface-node', 'Node')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>No change (was for a long time removed from it).</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM3 Core', 'core.html#Node3-isSameNode', 'Node.isSameNode()')}}</td>
   <td>{{Spec2('DOM3 Core')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="sect1">&nbsp;</h2>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("1.0")}}<br />
    Removed in {{CompatGeckoDesktop("10")}}<br />
    Readded in {{CompatGeckoDesktop("48")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("1.0")}}<br />
    Removed in {{CompatGeckoMobile("10")}}<br />
    Readded in {{CompatGeckoMobile("48")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("Node.isEqualNode()")}}</li>
</ul>
Revert to this revision