JS Compatibility Event

Source: Internet
Author: User

//browser detection
(function () {
window.sys = {};
var ua = Navigator.userAgent.toLowerCase ();
var s;
(s = Ua.match (/msie [\d.] +)/)) ? sys.ie = s[1]:
(s = Ua.match (/firefox\/([\d]) +)/)) ? Sys.firefox = s[1]:
(s = Ua.match (/chrome\/([\d]) +)/)) ? Sys.chrome = s[1]:
(s = Ua.match (/opera\/.*version\/([\d]) +)/)) ? Sys.opera = s[1]:
(s = Ua.match (/version\/([\d]) +). *safari/))? Sys.safari = S[1]: 0;

if (/webkit/.test (UA)) Sys.webkit = Ua.match (/webkit\/([\d]. +)/) [1];

//dom Loading
function adddomloaded (FN) {
var isReady = false;
var timer = null;
function Doready () {
if (timer) clearinterval (timer);
if (isReady) return;
IsReady = true;
fn ();
}

if ((Sys.opera && Sys.opera < 9) | | (Sys.firefox && Sys.firefox < 3) | | (Sys.webkit && Sys.webkit < 525)) {
//Whichever you use, it's basically unnecessary.
/*timer = setinterval (function () {
if (/loaded|complete/.test (document.readystate)) {//loaded is partially loaded, it is possible that only the DOM is loaded and complete is fully loaded, similar to onload
Doready ();
}
}, 1); * /

timer = setinterval (function () {
if (document && document.getElementById && document.getelementsbytagname && document.body ) {
Doready ();
}
}, 1);
} else if (Document.addeventlistener) {//w3c
addevent (document, ' domcontentloaded ', function () {
fn ();
removeevent (document, ' domcontentloaded ', Arguments.callee);
});
} else if (sys.ie && sys.ie < 9) {
var timer = null;
timer = setinterval (function () {
try {
document.documentElement.doScroll (' left ');
Doready ();
} catch (e) {};
}, 1);
}
}

//cross-browser add event bindings
function addevent (obj, type, fn) {
if (typeof obj.addeventlistener! = ' undefined ') {
obj.addeventlistener (Type, FN, false);
} else {
//Create a hash table that holds events (hash table)
if (!obj.events) obj.events = {};
//execution at first execution
if (!obj.events[type]) {
//Create an array that holds the event handler function
Obj.events[type] = [];
//The first event handler is first stored in the first position
if (obj[' on ' + type]) obj.events[type][0] = fn;
} else {
//The same registration function is masked, not added to the counter
if (addevent.equal (Obj.events[type], FN)) return false;
}
//From the second start we use event counters to store
obj.events[type][addevent.id++] = fn;
//Execute event handler function
obj[' on ' + type] = Addevent.exec;
}
}

Assign a counter to each event
Addevent.id = 1;

Execute event handler function
addevent.exec = function (event) {
var e = Event | | Addevent.fixevent (window.event);
var es = This.events[e.type];
for (var i in es) {
Es[i].call (this, e);
}
};

Masking with the same registration function
Addevent.equal = function (es, FN) {
for (var i in es) {
if (es[i] = = fn) return true;
}
return false;
}

Pair the event objects commonly used by IE into the internet
Addevent.fixevent = function (event) {
Event.preventdefault = AddEvent.fixEvent.preventDefault;
Event.stoppropagation = addEvent.fixEvent.stopPropagation;
Event.target = event.srcelement;
return event;
};

IE block default behavior
AddEvent.fixEvent.preventDefault = function () {
This.returnvalue = false;
};

IE Cancel bubbling
AddEvent.fixEvent.stopPropagation = function () {
This.cancelbubble = true;
};


//cross-browser Delete event
function removeevent ( obj, type, fn) {
if (typeof obj.removeeventlistener! = ' undefined ') {
obj.removeeventlistener (Type, FN, false);
} else {
if (obj.events) {
Span style= "font-size:16px" >for (var i in Obj.events[type]) {
if (obj.events[ Type][i] = = fn) {
delete obj.events[type][i];
}
}
}< /span>
}
}


//get viewport size across browsers
function Getinner () {
if (typeof window.innerwidth! = ' undefined ') {
return {
width:window.innerwidth,
height:window.innerheight
}
} else {
return {
width:document.documentelement.clientwidth,
height:document.documentelement.clientheight

}
}< /span>

//get scroll bar position across browsers
function Getscroll () {
return {
top: Document.documentElement.scrollTop | | Document.body.scrollTop,
left:document.documentelement.scrollleft | | Document.body.scrollLeft
}
}


//cross-browser get style
function GetStyle ( Element, attr) {
var value;
if (typeof window.getcomputedstyle! = ' undefined ') {//w3c
value = window.getComputedStyle (element, NULL) [attr];
} else if (typeof element.currentstyle! = ' undeinfed ') {//ie
value = element.currentstyle[attr];
}
return value;
}


Determine if class exists
function hasclass (element, ClassName) {
Return Element.className.match (New RegExp (' (\\s|^) ' +classname + ' (\\s|$) '));
}


Add a link rule across browsers
function Insertrule (sheet, selectortext, csstext, position) {
if (typeof sheet.insertrule! = ' undefined ') {//W3C
Sheet.insertrule (Selectortext + ' {' + csstext + '} ', position);
} else if (typeof sheet.addrule! = ' undefined ') {//ie
Sheet.addrule (Selectortext, csstext, position);
}
}

Move the link rule across browsers
function DeleteRule (sheet, index) {
if (typeof sheet.deleterule! = ' undefined ') {//W3C
Sheet.deleterule (index);
} else if (typeof sheet.removerule! = ' undefined ') {//ie
Sheet.removerule (index);
}
}

Get innertext across browsers
function Getinnertext (Element) {
return (typeof element.textcontent = = ' string ')? Element.textContent:element.innerText;
}

//cross-browser settings innertext
function Setinnertext (elememt, text) {
if (typeof element.textcontent = = ' string ') {
element.textcontent = text;
} else {
element.innertext = text;
}
}

Gets the position of an element to the outermost vertex
function OffsetTop (Element) {
var top = Element.offsettop;
var parent = element.offsetparent;
while (parent! = NULL) {
Top + = Parent.offsettop;
parent = parent.offsetparent;
}
return top;
}

Delete left after space
function Trim (str) {
Return Str.replace (/(^\s*) | ( \s*$)/g, ");
}

Whether a certain value exists in an array
function InArray (array, value) {
for (var i in array) {
if (array[i] = = = value) return true;
}
return false;
}

Gets the index of the previous node of a node
function Previndex (current, parent) {
var length = Parent.children.length;
if (current = = 0) return length-1;
return parseint (current)-1;
}

Gets the index of the next node of a node
function Nextindex (current, parent) {
var length = Parent.children.length;
if (current = = Length-1) return 0;
return parseint (current) + 1;
}

Scroll bar pinning
function Fixedscroll () {
Window.scrollto (Fixedscroll.left, fixedscroll.top);
}

Block default behavior
function Predef (e) {
E.preventdefault ();
}

//Create Cookies
function Setcookie (name, value, expires, path, domain, secure) {
var cookietext = encodeuricomponent (name) + ' = ' + encodeuricomponent (value);
if (expires instanceof Date) {
Cookietext + = '; expires= ' + expires;
}
if (path) {
Cookietext + = '; expires= ' + expires;
}
if (domain) {
Cookietext + = '; domain= ' + domain;
}
if (secure) {
Cookietext + = '; secure ';
}
document.cookie = Cookietext;
}

//Get Cookies
function GetCookie (name) {
var cookiename = encodeuricomponent (name) + ' = ';
var cookiestart = document.cookie.indexOf (cookiename);
var cookievalue = null;
if (Cookiestart >-1) {
var cookieend = document.cookie.indexOf ('; ', cookiestart);
if (cookieend = =-1) {
cookieend = document.cookie.length;
}
cookievalue = decodeuricomponent (document.cookie.substring (Cookiestart + cookiename.length, cookieend));
}
return cookievalue;
}

Delete Cookies
function Unsetcookie (name) {
Document.cookie = name + "=; expires= "+ new Date (0);
}

JS Compatibility Event

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.