admin管理员组

文章数量:1202392

Trying determinate DOM Element by simple check

isElement = SomeThing instanceof Element

works in main document, but not on (all?) nodes in iframe.

Example output (Google Chrome): (mdiv is DIV in main document, idiv is DIV in iframe)

OMGWTF 
ok:  mdiv instanceof Element ... true ... [object HTMLDivElement]
ok:  mdiv instanceof Object ... true ... [object HTMLDivElement]
ko:  idiv instanceof Element ... false ... [object HTMLDivElement]
KO :  idiv instanceof Object ... false ... [object HTMLDivElement] 

There are different javascript implementations for main document and for iframe documents ???

Please explain me what's wrong.

Example: (.xhtml)

Code: obalka.xhtml (main document)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="">
  <head>
   <title>Obalka</title>
  </head>
  <body>
    <div id="auto_filled_commands_container">
MAIN div id="auto_filled_commands_container"<br/>
        <iframe id="auto_filled_commands_iframe" src='dopis.xhtml' style="width:98%;height:98%;"/>
    </div>
<div>
<textarea id="OMGWTF" style="width:700px;height:200px">
mdiv = document.getElementById("auto_filled_commands_container");
ifram = document.getElementById("auto_filled_commands_iframe");
idiv = ifram.contentDocument.getElementById('auto_filled_commands');
OMGWTF = "OMGWTF \n"
+"ok:  mdiv instanceof Element ... "+(mdiv instanceof Element)+" ... "+mdiv+"\n"
+"ok:  mdiv instanceof Object ... "+(mdiv instanceof Object)+" ... "+mdiv+"\n"
+"ko:  idiv instanceof Element ... "+(idiv instanceof Element)+" ... "+idiv+"\n"
+"KO :  idiv instanceof Object ... "+(idiv instanceof Object)+" ... "+idiv+"\n"
;
document.getElementById('result_txta').value = OMGWTF;
</textarea>
<br/><input type="button" value="Eval code in upper textarea (to bypass possible developer tools error)" onclick="
    eval(document.getElementById('OMGWTF').value);
"/><b>(2.)</b><br/>
<textarea id="result_txta" style="width:700px;height:100px">
</textarea>
</div>
  </body>
</html>

Code: Dopis.xhtml (inner frame document)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="">
  <head>
   <title>Dopis</title>
  </head>
  <body>
    <div id="auto_filled_commands">
IFRAME div id="auto_filled_commands"<br/>
        <div id="afc_formular">
IFRAME div id="afc_formular"<br/>
<input id="cmnd" type="text" value="input id='cmnd'" />
<br/><input type="button" value="Click to get browser userAgent" onclick="
    var preEl = this.ownerDocument.getElementById('navUserAgent');
    var cdataEl = preEl.firstChild || preEl;    /* IE don't know CDATA ?! */
    cdataEl.textContent=navigator.userAgent;
"/><b>(1.)</b>
<pre id="navUserAgent"><![CDATA[
]]></pre>
        </div>
    </div>
  </body>
</html>

Results image (in IE, Chrome, Firefox, Opera)
(source: sbmintegral.sk)

Trying determinate DOM Element by simple check

isElement = SomeThing instanceof Element

works in main document, but not on (all?) nodes in iframe.

Example output (Google Chrome): (mdiv is DIV in main document, idiv is DIV in iframe)

OMGWTF 
ok:  mdiv instanceof Element ... true ... [object HTMLDivElement]
ok:  mdiv instanceof Object ... true ... [object HTMLDivElement]
ko:  idiv instanceof Element ... false ... [object HTMLDivElement]
KO :  idiv instanceof Object ... false ... [object HTMLDivElement] 

There are different javascript implementations for main document and for iframe documents ???

Please explain me what's wrong.

Example: (http://www.sbmintegral.sk/GITHUB/OMGWTF/obalka.xhtml)

Code: obalka.xhtml (main document)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Obalka</title>
  </head>
  <body>
    <div id="auto_filled_commands_container">
MAIN div id="auto_filled_commands_container"<br/>
        <iframe id="auto_filled_commands_iframe" src='dopis.xhtml' style="width:98%;height:98%;"/>
    </div>
<div>
<textarea id="OMGWTF" style="width:700px;height:200px">
mdiv = document.getElementById("auto_filled_commands_container");
ifram = document.getElementById("auto_filled_commands_iframe");
idiv = ifram.contentDocument.getElementById('auto_filled_commands');
OMGWTF = "OMGWTF \n"
+"ok:  mdiv instanceof Element ... "+(mdiv instanceof Element)+" ... "+mdiv+"\n"
+"ok:  mdiv instanceof Object ... "+(mdiv instanceof Object)+" ... "+mdiv+"\n"
+"ko:  idiv instanceof Element ... "+(idiv instanceof Element)+" ... "+idiv+"\n"
+"KO :  idiv instanceof Object ... "+(idiv instanceof Object)+" ... "+idiv+"\n"
;
document.getElementById('result_txta').value = OMGWTF;
</textarea>
<br/><input type="button" value="Eval code in upper textarea (to bypass possible developer tools error)" onclick="
    eval(document.getElementById('OMGWTF').value);
"/><b>(2.)</b><br/>
<textarea id="result_txta" style="width:700px;height:100px">
</textarea>
</div>
  </body>
</html>

Code: Dopis.xhtml (inner frame document)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Dopis</title>
  </head>
  <body>
    <div id="auto_filled_commands">
IFRAME div id="auto_filled_commands"<br/>
        <div id="afc_formular">
IFRAME div id="afc_formular"<br/>
<input id="cmnd" type="text" value="input id='cmnd'" />
<br/><input type="button" value="Click to get browser userAgent" onclick="
    var preEl = this.ownerDocument.getElementById('navUserAgent');
    var cdataEl = preEl.firstChild || preEl;    /* IE don't know CDATA ?! */
    cdataEl.textContent=navigator.userAgent;
"/><b>(1.)</b>
<pre id="navUserAgent"><![CDATA[
]]></pre>
        </div>
    </div>
  </body>
</html>

Results image (in IE, Chrome, Firefox, Opera)
(source: sbmintegral.sk)

Share Improve this question edited Sep 22, 2019 at 5:26 Glorfindel 22.6k13 gold badges89 silver badges118 bronze badges asked Oct 8, 2014 at 3:06 supipdsupipd 3293 silver badges10 bronze badges 12
  • What's "Element"? in DOM level 1, the basicmost of DOM specs from 1998, HTML elements implement the HTMLElement interface; there is no, nor has there ever been, an official Element interface. With that said, why on earth are you still using XHTML? We got rid of that ages ago. – Mike 'Pomax' Kamermans Commented Oct 8, 2014 at 3:42
  • Element is constructor of HTMLElement ! – supipd Commented Oct 8, 2014 at 6:14
  • Element is constructor of HTMLElement ! Object is constructor of any object, that's why all objects must be instanceof Object. Why XHTML ? ... because HTML has no CDATA XHTML: <pre><![CDATA[ 1 < 3 < 134 ]]> </pre> HTML : <pre> 1 &lt 3 &lt 134 </pre> what is cleaner ? If HTML is for lazy peoples which are not able to correctly write XML code, XHTML offers (as XML) many many advantages as f.e. XML islands – supipd Commented Oct 8, 2014 at 6:21
  • Not sure what you're talking about there: you don't need CDATA in HTML. If you need verbatim code (which most of the time you absolutely do not), you just stick it inside a <textarea> and style it appropriately with CSS. Done: no weird conflicts about < needing to be an HTML entity or xml-style cdata escaping. HTML is not "for lazy people", it's simply not XML. It's hypertext markup, which has very different parsing rules. – Mike 'Pomax' Kamermans Commented Oct 9, 2014 at 1:14
  • Not space to debate. I simple need XML and its powerfull technologies like (XML) SVG, (XML) X3D, XPath searching, XSLT in-browser transformations, XML data islands for Rich Internet Applications ... to dynamically change content according incomming data and user activities ... all without need background server support. After 2 years fighting with problems in HTML, I'm now working with XHTML, and ... I'm happy ... Be too. – supipd Commented Oct 9, 2014 at 5:54
 |  Show 7 more comments

1 Answer 1

Reset to default 25

There are different javascript implementations for main document and for iframe documents ???

Yes. The iframe has its own environment with its own globals. Try

var iwin = ifram.contentWindow;
idiv instanceof iwin.HTMLElement; // true
idiv instanceof iwin.Object; // true

本文标签: javascriptinstanceof HTMLElement in IFRAME is not Element or ObjectStack Overflow