//trim
function trim(str){
str = str.replace(/(^\s*)|(\s*$)/g,"");
return str;
}
//write cookie
function writeCookie(cookieName, cookieValue, cookiePath, expireMilliseconds){
var todayDate = new Date();
var expireSetting = "";
if(expireMilliseconds != 0){
todayDate.setTime(todayDate.getTime() + expireMilliseconds);
expireSetting = "expires=" + todayDate.toGMTString() + ";";
}
var pathSetting = "";
if(cookiePath != ""){
pathSetting = "path=" + cookiePath + ";";
}
document.cookie = cookieName + "=" + cookieValue + ";" + pathSetting + expireSetting;
}
//read cookie
function readCookie(cookieName){
var cookieArray = document.cookie.split(";");
for(var i = 0 ; i < cookieArray.length ; i++){
var nameAndValue = cookieArray[i].split("=");
if(trim(nameAndValue[0]) == cookieName && nameAndValue.length == 2){
return trim(nameAndValue[1]);
}
}
return "";
}
//delete cookie
function deleteCookie(cookieName, cookiePath){
writeCookie(cookieName, "", cookiePath, -1 * 24 * 60 * 60 * 1000);
}
//get key code according to browser
function getKeyCode(eventToGetKeyCode){
var keyCodeFromEvent;
if(window.event){ // IE
keyCodeFromEvent = eventToGetKeyCode.keyCode;
}else if(eventToGetKeyCode.which) { // Netscape/Firefox/Opera
keyCodeFromEvent = eventToGetKeyCode.which;
}
return keyCodeFromEvent;
}
//event-adding function for cross browser
function addEventHandler(obj, eventName, handler){
if(document.attachEvent){
obj.attachEvent("on" + eventName, handler);
}else if(document.addEventListener){
obj.addEventListener(eventName, handler, false);
}
}
//event-removing function for cross browser
function removeEventHandler(obj, eventName, handler){
if(document.detachEvent){
obj.detachEvent("on" + eventName, handler);
}else if(document.removeEventListener){
obj.removeEventListener(eventName, handler, false);
}
}
//get active object function for cross browser
function getActivatedObject(e){
var obj;
if(!e){
obj = window.event.srcElement; //old explorer
}else if(e.srcElement){
obj = e.srcElement; //ie7 or later
}else{
obj = e.target; //dom level 2
}
return obj;
}
//clear child nodes
function clearChildNodes(nodeToClear){
for(var i = nodeToClear.childNodes.length; i > 0 ; i--){
nodeToClear.removeChild(nodeToClear.childNodes[i - 1]);
}
}
//set inner text
function setInnerText(nodeToSet, textToSet){
clearChildNodes(nodeToSet);
//var textArray = textToSet.toString().split("<br>");
var textArray = textToSet.toString().split(/<br>/i);
for(i = 0 ; i < textArray.length ; i++){
if(i > 0){ nodeToSet.appendChild(document.createElement("br")); }
var newTextNode = document.createTextNode(textArray[i]);
nodeToSet.appendChild(newTextNode);
}
}
Comments
Post a Comment