/    Sign up×
Community /Pin to ProfileBookmark

Checking type is nodeList in IE

Anyway clever ways of checking whether an object is a nodelist in IE. There appears to only be two properties item and length.

This is what I’m working on

[CODE]// Some weirdness in IE regarding nodesList and typeof ‘item’ returning ‘Object’
// even though when alerted it returns ‘function (){…}’.
// Therefore having to use regExp.test() to check whether it’s a function instead.
// Note: _isNodeList isn’t full proof. An object with the properties
// {length: x, item : function(){}} will pass and return length.

var _isNodeList = function(obj){
var objType = {}.toString.call(obj);
return (objType === ‘[object NodeList]’ ||
objType === ‘[object HTMLCollection]’ ||
objType === ‘[object Object]’ && /^s?function/.test(obj.item))
&& obj.length; // returns length of nodeList if true
};[/CODE]

Cheers

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rpg2009authorDec 17.2010 — I think that's called irony. I mean 'Any clever ways':o
Copy linkTweet thisAlerts:
@TcobbDec 17.2010 — All objects have a constructor property. I understand that the exact wording may vary depending upon the OS, so you do something like:

[CODE]function reallyNodeList(input){
var x = document.getElementsByTagName('body'); //this will make x a nodelist
if (x.constructor === input.constructor){
return true;
}
else {
return false;
}
}[/CODE]
Copy linkTweet thisAlerts:
@rpg2009authorDec 17.2010 — Nice idea, but failed in firefox

console.log(reallyNodeList({})); // true

Thanks though man?
Copy linkTweet thisAlerts:
@TcobbDec 17.2010 — Sorry about that. The only other thing I can think of right off hand is this:

[CODE]function reallyNodeList(input){
var x = [];
var z = "string";
if(z.constructor == input.constructor){return false;}
if (input.length && (input.constructor != x.constructor)){
return true;
}
else{
return false;}
}[/CODE]


It does return 'false' if you send it '{}' as the argument. Other than an array, string and a nodelist does anything else have a length property?

And then of course you could run through each element to see if it exists as a reference, but simple arrays can hold references too---in which case they might as well be treated as a nodelist.
Copy linkTweet thisAlerts:
@rpg2009authorDec 17.2010 — Other than an array, string and a nodelist does anything else have a length property?[/QUOTE]

Yes a function

function test(a,b,c){};

console.log(test.length) // 3

An object

myObject = {length: 5};

And then of course you could run through each element to see if it exists as a reference, but simple arrays can hold references too---in which case they might as well be treated as a nodelist.[/QUOTE]

I'm trying to write a function for converting nodelists and object to arrays

Note: 5th line should read
[CODE]objType === '[object Object]' && /^s?function/.test(obj.item))[/CODE]

[code=php]var _isNodeList = function(obj){
var objType = {}.toString.call(obj);
return (objType === '[object NodeList]' ||
objType === '[object HTMLCollection]' ||
objType === '[object Object]' && /^s?function/.test(obj.item))
&& obj.length; // returns length of nodeList if true

};

var _toString = {}.toString,
_slice = [].slice;

// ------- toArray -------
var toArray = function (obj /* HTMLCollection or Object */){

var copy = [],
len = _isNodeList(obj),
oType = _toString.call(obj);

if (len){
try {
copy = _slice.call(obj); return copy;
}
catch(e) {
while (len--) copy[len] = obj[len]; return copy;
}
} else if ( oType === '[object Object]' ){
for (var prop in obj){
if (obj.hasOwnProperty(prop)) copy.push(obj[prop]);
}
return copy;
}

throw new TypeError ( 'Object type ' + oType + ' not supported!!' );
};[/code]


It's fun'n'games?
×

Success!

Help @rpg2009 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...