Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Created February 25, 2016 20:55
Show Gist options
  • Save JDMcKinstry/e4b2bd89876ca48f461b to your computer and use it in GitHub Desktop.
Save JDMcKinstry/e4b2bd89876ca48f461b to your computer and use it in GitHub Desktop.
Easy way to test if created element is a valid Tag for current browser
Object.defineProperty && !Element.prototype.hasOwnProperty("isValidTag") ? Object.defineProperty(Element.prototype, "isValidTag", {
value: function(b) {
if (this.tagName) {
var c = "acronym applet basefont bgsound big blink center dir font frame frameset hgroup isindex listing marquee multicol nextid nobr noembed noframes plaintext spacer strike tt xmp".split(" "),
a = this.tagName;
return a.match(/[^a-zA-Z0-9]/) ? !1 : -1 !== b && -1 !== c.indexOf(a.toLowerCase()) ? b || !1 : "[object HTMLUnknownElement]" !== Object.prototype.toString.call(document.createElement(a))
}
return !1
}
}) :
Element.prototype.isValidTag = function(b) {
if (this.tagName) {
var c = "acronym applet basefont bgsound big blink center dir font frame frameset hgroup isindex listing marquee multicol nextid nobr noembed noframes plaintext spacer strike tt xmp".split(" "),
a = this.tagName;
return a.match(/[^a-zA-Z0-9]/) ? !1 : -1 !== b && -1 !== c.indexOf(a.toLowerCase()) ? b || !1 : "[object HTMLUnknownElement]" !== Object.prototype.toString.call(document.createElement(a))
}
return !1
};
// us as simple as
if (!document.createElement('blink').isValidTag()) {
var head = document.head || document.getElementsByTagName("head")[0],
style = document.createElement("style");
/* Standard (Mozilla) */
style.appendChild(document.createTextNode("@keyframes blink { from { opacity: 1; } to { opacity: 0; } }"));
/* Chrome & Safari */
style.appendChild(document.createTextNode("@-webkit-keyframes blink { from { opacity: 1; } to { opacity: 0; } }"));
style.appendChild(document.createTextNode("blink { -webkit-animation: blink 600ms infinite; animation: blink 600ms infinite; text-decoration: blink; }"));
head.appendChild(style);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment