Check if an variable is refer to designate object in JavaScript
Suppose I have an variable assign to 开发者_如何学编程an "video" tag, how can I check if this variable is refer to the object of "HTMLVideoElement"
var video = document.getElementById('video')
if (video != object HTMLUnknownElement) {
//some code here
}
What is the code to replace with object HTMLUnknownElement above?
Thanks
typeof is pretty useless, it'll just tell you that it's an object. You'd do better with instanceof: video instanceof HTMLVideoElement
.
EDIT: Some browsers don't know what an HTMLVideoElement is, so to protect against them throwing an exception here you should check for it: typeof HTMLVideoElement !== "undefined" && video instanceof HTMLVideoElement
.
精彩评论