常用JS片段
时间:2010-08-31 来源:喵 喵
摘自<<javascript精粹>>一书,代码片段不断更新.
function getInternalText(target)
{
var elementChildren = target.childNodes;
var internalText = "";
for (var i = 0; i < elementChildren.length; i++)
{
if (elementChildren[i].nodeType == 3)
{
if (!/^\s*$/.test(elementChildren[i].nodeValue))
{
internalText += elementChildren[i].nodeValue;
}
}
else
{
internalText += getInternalText(elementChildren[i]);
}
}
return internalText;
}
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 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 stopDefaultAction(event)
{
event.returnValue = false;
if (typeof event.preventDefault != "undefined")
{
event.preventDefault();
}
return true;
}
function getElementsByAttribute(attribute, attributeValue)
{
var elementArray = new Array();
var matchedArray = new Array();
if (document.all)
{
elementArray = document.all;
}
else
{
elementArray = document.getElementsByTagName("*");
}
for (var i = 0; i < elementArray.length; i++)
{
if (attribute == "class")
{
var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
if (elementArray[i].className.match(pattern))
{
matchedArray[matchedArray.length] = elementArray[i];
}
}
else if (attribute == "for")
{
if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
{
if (elementArray[i].htmlFor == attributeValue)
{
matchedArray[matchedArray.length] = elementArray[i];
}
}
}
else if (elementArray[i].getAttribute(attribute) == attributeValue)
{
matchedArray[matchedArray.length] = elementArray[i];
}
}
return matchedArray;
}
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;
}
{
var elementChildren = target.childNodes;
var internalText = "";
for (var i = 0; i < elementChildren.length; i++)
{
if (elementChildren[i].nodeType == 3)
{
if (!/^\s*$/.test(elementChildren[i].nodeValue))
{
internalText += elementChildren[i].nodeValue;
}
}
else
{
internalText += getInternalText(elementChildren[i]);
}
}
return internalText;
}
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 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 stopDefaultAction(event)
{
event.returnValue = false;
if (typeof event.preventDefault != "undefined")
{
event.preventDefault();
}
return true;
}
function getElementsByAttribute(attribute, attributeValue)
{
var elementArray = new Array();
var matchedArray = new Array();
if (document.all)
{
elementArray = document.all;
}
else
{
elementArray = document.getElementsByTagName("*");
}
for (var i = 0; i < elementArray.length; i++)
{
if (attribute == "class")
{
var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
if (elementArray[i].className.match(pattern))
{
matchedArray[matchedArray.length] = elementArray[i];
}
}
else if (attribute == "for")
{
if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
{
if (elementArray[i].htmlFor == attributeValue)
{
matchedArray[matchedArray.length] = elementArray[i];
}
}
}
else if (elementArray[i].getAttribute(attribute) == attributeValue)
{
matchedArray[matchedArray.length] = elementArray[i];
}
}
return matchedArray;
}
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;
}
相关阅读 更多 +