Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Object.prototype.toString.call() to return object type with Javascript - not working in IE
    primarykey
    data
    text
    <p>Hopefully I can ask this in an understandable way...</p> <p>Overall, I am trying to determine what type of object I am currently dealing with.</p> <p>I'm creating a collection (HTML is example, not literal) and I need to filter my collection to certain elements eg:</p> <pre><code> &lt;div id="tabContentWrap"&gt; &lt;div id="tab"&gt; &lt;a href="http://somelink"&gt;Link Element&lt;/a&gt;&lt;img src="img.jpg" alt="img" /&gt; &lt;select id="my_select"&gt;&lt;option value="1"&gt;1&lt;/option&gt;&lt;/select&gt; &lt;/div&gt; &lt;/div&gt; function getFilteredElements() { var tabContent = getElementsByClass("tabContentWrap", document.getElementById(tabWrapId), "div"); for (var j = 0; j &lt; tabContent.length; j++){ tabContentLinks = tabContent[j].getElementsByTagName('*'); for (var k = 0; k &lt; tabContentLinks.length; k++){ // Here i attempt to filter the collection if (tabContentLinks[k] == '[object HTMLSelectElement]') { alert("found select list"); } } } } </code></pre> <p>Which works fine in Mozilla but not in Internet Explorer 8, <code>tabContentLinks[k]</code> returns <code>[object]</code> instead of <code>[object 'ObjectType']</code></p> <p>So I investigated and discovered that you can use <code>Object.prototype.toString.call(object)</code> to get the object type, which again works fine in Mozilla but returns <code>[object Object]</code> in IE8...</p> <p>I call</p> <pre><code>get_type(tabContentsLink[k]); </code></pre> <p>which runs the following function:</p> <pre><code>function get_type(thing){ if (thing === null) return "[object Null]"; // special case return Object.prototype.toString.call(thing); } </code></pre> <p>But this just returns <code>[object Object]</code></p> <p>Does <code>Object.prototype.toString.call()</code> ever return the type of object in IE or am I very far off and barking up a lamppost instead of a tree?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload