admin管理员组文章数量:1244245
Is there any Native Javascript Functions to check if html tag exists?
I mean :
var tag = "div";
alert(isValidTag(tag)) // true;
alert(isValidTag("foo")) // false;
If there is no native function for that, I will keep my function :
function isValidTag(tagName) {
var tags = ["div","span","a","link" ... "body"];
for(var i=0, len = tags.length; i++ < len; ) {
if(tags[i] == tagName) return true;
}
return false;
}
Is there any Native Javascript Functions to check if html tag exists?
I mean :
var tag = "div";
alert(isValidTag(tag)) // true;
alert(isValidTag("foo")) // false;
If there is no native function for that, I will keep my function :
function isValidTag(tagName) {
var tags = ["div","span","a","link" ... "body"];
for(var i=0, len = tags.length; i++ < len; ) {
if(tags[i] == tagName) return true;
}
return false;
}
Share
Improve this question
asked Jul 25, 2012 at 12:29
JohnJohn
7,91017 gold badges67 silver badges96 bronze badges
3
- 6 If you care about performance, you'd be better off making the "tags" variable be an object initialized outside that function, and with the tag names being keys. Then you would not have to iterate through the array to make the determination of whether a string is or is not a tag. – Pointy Commented Jul 25, 2012 at 12:33
-
@Pointy +1. Move the
var tags
outside of theisValidTag
scope so you are not declaring it every time you canisValidTag
– Sphvn Commented Jul 25, 2012 at 12:36 - Instead of iterating, you can check (indexOf > -1) – Samet DEDE Commented Jul 25, 2012 at 12:45
4 Answers
Reset to default 6No. JS has nothing HTML specific in it at all, and DOM doesn't add anything like that.
This might not be as efficient, I haven't done any time trials on it, but it can still be a good alternative to having to maintain a list of all the possible values yourself.
var isHTML = (function() {
var unknown = '[object HTMLUnknownElement]', overrides = {CANVAS:1,VIDEO:1}; //html5 elements. Some browsers don't support these.
return function(tag) {
return overrides[tag = tag.toUpperCase()] || (!overrides.hasOwnProperty(tag) && (overrides[tag] = (document.createElement(tag).toString() !== unknown)));
};
})();
This method will first check for a cached result, and if there isn't one it will determine a result based on the browser's own document.createElement
. If the browser doesn't support it then we can safely assume it isn't an html tag.
Some sample outputs:
isHTML('curve'); //false
isHTML('div'); //true
isHTML('DIV'); //true
isHTML('tbody'); //true
isHTML('object'); //true
isHTML('document'); //false
isHTML('html'); //true
isHTML('svg'); //false
isHTML('rect'); //false
How about this:
tags = 'a b body...'.split(' ');
function isTag(tag) {
return tags.indexOf(tag.trim().toLowerCase()) > -1;
}
var tags = {a: true, b: true, /*...,*/ body: true};
function isTag(_tag)
{
return tags[_tag];
}
Thank you, @rsp, for suggesting this simplification
本文标签: javascriptCheck if string is valid html tagStack Overflow
版权声明:本文标题:javascript - Check if string is valid html tag. - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740116947a2227262.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论