自分で整理したセットのjavascript小型共用倉庫
11137 ワード
/**
* @author zjq common use jslib
*/
//
function addLoadListener(fn){
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('load', fn, false);
}
else
if (typeof document.addEventListener != 'undefined') {
document.addEventListener('load', fn, false);
}
else
if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onload', fn);
}
else {
var oldfn = window.onload;
if (typeof window.onload != 'function') {
window.onload = fn;
}
else {
window.onload = function(){
oldfn();
fn();
};
}
}
}
//
function attachEventListener(target, eventType, functionRef, capture){
if (typeof target.addEventListener != "undefined") {
target.addEventListener(eventType, functionRef, capture);
}
else
if (typeof target.attachEvent != "undefined") {
target.attachEvent("on" + eventType, functionRef);
}
else {
eventType = "on" + eventType;
if (typeof target[eventType] == "function") {
var oldListener = target[eventType];
target[eventType] = function(){
oldListener();
return functionRef();
}
}
else {
target[eventType] = functionRef;
}
}
return true;
}
//
function detachEventListener(target, eventType, functionRef, capture){
if (typeof target.removeEventListener != "undefined") {
target.removeEventListener(eventType, functionRef, capture)
}
else
if (typeof target.detachEvent != "undefined") {
target.detachEvent("on" + eventType, functionRef);
}
else {
target["on" + eventType] = null;
}
return true;
};
//for
function getScrollPosition(){
var position = [0, 0];
if (typeof window.pageXOffset != "undefined") {
position = [window.pageXOffset, window.pageYOffset];
//for ko,safri,firefox ,opera;
}
else {
if (typeof document.documentElement.scrollTop != "undefined" && document.documentElement.scrollTop > 0) {
//for ie, firefox ;
position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
}
else
if (typeof document.body.scrollLeft != "undefined") {
position = [document.body.scrollLeft, document.body.scrollTop];
}
}
return position;
}
//for
function getCursorPosition(event){
//for ie event
if (typeof event == "undefined") {
event = window.event;
}
var scrollPostion = getScrollPosition();
// alert(scrollPostion[0]);
// alert(scrollPostion[1]);
var aCursorPosition = [0, 0];
if (typeof pageX != "undefined" && typeof event.x != "undefined") //event.x for Mac ie pageX ,
// , x.
{
aCursorPosition[0] = event.pageX;
aCursorPosition[1] = event.pageY;
}
else {
aCursorPosition[0] = event.clientX + scrollPostion[0];
aCursorPosition[1] = event.clientY + scrollPostion[1];
}
//var oResult = document.getElementById("result");
// oResult.innerHTML = "X :" + aCursorPosition[0] + '\r' + "Y :" + aCursorPosition[1];
return true;
}
//for
function retrieveComputedStyle(element, styleProperty){
var computedStyle = null;
if (typeof element.currentStyle != "undefined") {
computedStyle = element.currentStyle;
}
else {
computedStyle = document.defaultView.getComputedStyle(element, null);
}
return computedStyle[styleProperty];
}
function getElePosition(theEle){
var xTheEle = 0;
var yTheEle = 0;
while (theEle != null) {
xTheEle += theEle.offsetLeft;
yTheEle += theEle.offsetTop;
theEle = theEle.offsetParent;
}
return [xTheEle, yTheEle];
}
//for
function identifyBrowser(){
var agent = navigator.userAgent.toLowerCase();
if (typeof navigator.vendor != "undefined" && navigator.vendor == "KDE" && typeof window.sidebar != "undefined") {
return "kde";
}
else
if (typeof window.opera != "undefined") {
var version = parseFloat(agent.replace(/.*opera[\/ ]([^ $]+).*/, "$1"));
if (version >= 7) {
return "opera7";
}
else
if (version >= 5) {
return "opera5";
}
return false;
}
else
if (typeof document.all != "undefined") {
if (typeof document.getElementById != "undefined") {
var browser = agent.replace(/.*ms(ie[\/ ][^ $]+).*/, "$1").replace(/ /, "");
if (typeof document.uniqueID != "undefined") {
if (browser.indexOf("5.5") != -1) {
return browser.replace(/(.*5\.5).*/, "$1");
}
else {
return browser.replace(/(.*)\..*/, "$1");
}
}
else {
return "ie5mac";
}
}
return false;
}
else
if (typeof document.getElementById != "undefined") {
if (navigator.vendor.indexOf("Apple Computer, Inc.") != -1) {
if (typeof window.XMLHttpRequest != "undefined") {
return "safari1.2";
}
return "safari1";
}
else
if (agent.indexOf("gecko") != -1) {
return "mozilla";
}
}
return false;
};
//for
function identifyOS(){
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf("win") != -1) {
return "win";
}
else
if (agent.indexOf("mac")) {
return "mac";
}
else {
return "unix";
}
return false;
};
//for event
function getEventTarget(event){
var targetElement = null;
if (typeof event.target != "undefined") {
targetElement = event.target;
}
else {
targetElement = event.srcElement;
}
while (targetElement.nodeType == 3 && targetElement.parentNode != null) {
targetElement = targetElement.parentNode;
}
return targetElement;
};
function addEventHandler(oTarget, sEventType, fnHandler){
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
}
else
if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
}
else {
oTarget["on" + sEventType] = fnHandler;
}
};
function removeEventHandler(oTarget, sEventType, fnHandler){
if (oTarget.removeEventListener) {
oTarget.removeEventListener(sEventType, fnHandler, false);
}
else
if (oTarget.detachEvent) {
oTarget.detachEvent("on" + sEventType, fnHandler);
}
else {
oTarget["on" + sEventType] = null;
}
};
//
var $ = function(id){
return "string" == typeof id ? document.getElementById(id) : id;
};
//
function stopDefaultAction(event){
event.returnValue = false;
if (typeof event.preventDefault != "undefined") {
event.preventDefault();
}
return true;
};
// event
function getEventTarget(event){
var targetElement = null;
if (typeof event.target != "undefined") {
targetElement = event.target;
}
else {
targetElement = event.srcElement;
}
while (targetElement.nodeType == 3 && targetElement.parentNode != null) {
targetElement = targetElement.parentNode;
}
return targetElement;
};
//
function getPosition(theElement){
var positionX = 0;
var positionY = 0;
while (theElement != null) {
positionX += theElement.offsetLeft;
positionY += theElement.offsetTop;
theElement = theElement.offsetParent;
}
return [positionX, positionY];
};
自分で整理した共用倉庫.更新し続ける