add compat.js: some compat functions for older browsers - jscancer - Javascript crap (relatively small)
HTML git clone git://git.codemadness.org/jscancer
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 5e5b2ccae29afe5cb4e2f7faa2d39562ad8e2fcc
DIR parent 5ac77cb27101dd0822d685a87fae728ef0aa539f
HTML Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 28 May 2016 20:24:56 +0200
add compat.js: some compat functions for older browsers
this is an optional javascript which can be used for some compatibility
for older browsers.
Diffstat:
A compat.js | 51 +++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+), 0 deletions(-)
---
DIR diff --git a/compat.js b/compat.js
@@ -0,0 +1,51 @@
+if (typeof(Array) != "undefined" && typeof(Element) != "undefined" &&
+ typeof(Event) != "undefined" && typeof(String) != "undefined") {
+ if (!Array.prototype.indexOf)
+ Array.prototype.indexOf = function(s) {
+ if (!(s in this))
+ return -1;
+ for (var i = 0; i < this.length; i++)
+ if (this[i] === s)
+ return i;
+ return -1;
+ };
+ if (!Array.prototype.map)
+ Array.prototype.map = function(fn) {
+ var l = [];
+ for (var i = 0; i < this.length; i++)
+ l.push(fn(this[i]));
+ return l;
+ };
+ // TODO: fix for IE8.
+ if (!document.getElementsByClassName)
+ Element.prototype.getElementsByClassName = document.getElementsByClassName = function(classname) {
+ var els = this.getElementsByTagName("*"), l = [],
+ p = new RegExp("(^|\\s)" + classname + "(\\s|$)");
+ for (var i = 0; i < els.length; i++)
+ if (p.test(els[i].className))
+ l.push(els[i]);
+ return l;
+ };
+ if (!document.addEventListener)
+ // NOTE: capture is ignored if addEventListener is not supported.
+ if (this.attachEvent) // IE DOM
+ Element.prototype.addEventListener = document.addEventListener = function(ev, fn, capture) {
+ this.attachEvent("on" + ev, function(e) {
+ // fixup some standard event properties.
+ if (typeof(e) == "undefined")
+ e = window.event;
+ e.target = typeof(e.target) != "undefined" ? e.target : e.srcElement;
+ e.which = typeof(e.which) != "undefined" ? e.which : e.keyCode;
+ return fn.apply(this, arguments);
+ });
+ };
+ if (!Event.prototype.stopPropagation)
+ Event.prototype.stopPropagation = function() {
+ window.event.cancelBubble = true;
+ return false;
+ };
+ if (!String.trim)
+ String.prototype.trim = function() {
+ return this.replace(/^\s+|\s+$/g, "");
+ };
+}