//get key code
function getKeyCode(eventToGetKeyCode){
if(window.event){ // IE
return eventToGetKeyCode.keyCode;
}else if(eventToGetKeyCode.which) { // Netscape/Firefox/Opera
return eventToGetKeyCode.which;
}
}
//add event handler
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);
}
}
//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);
}
}
//print all object attributes
function printObjectAttribute(objToPrint){
var textToPrint = "";
for(var attr in objToPrint){
textToPrint += attr + " : " + objToPrint[attr] + "\n";
}
console.log("\n\n\n" + textToPrint + "\n\n\n");
}
function percentEncode(str){
if(str == null){ return ""; }
str = encodeURIComponent(str);
str = str.replace(/\!/g, "%21");
str = str.replace(/\*/g, "%2A");
str = str.replace(/\'/g, "%27");
str = str.replace(/\(/g, "%28");
str = str.replace(/\)/g, "%29");
str = str.replace(/\;/g, "%3B");
str = str.replace(/\:/g, "%3A");
str = str.replace(/\@/g, "%40");
//str = str.replace(/\&/g, "%26");
str = str.replace(/\=/g, "%3D");
str = str.replace(/\+/g, "%2B");
str = str.replace(/\$/g, "%24");
str = str.replace(/\,/g, "%2c");
str = str.replace(/\//g, "%2F");
str = str.replace(/\#/g, "%23");
str = str.replace(/\[/g, "%5B");
str = str.replace(/\]/g, "%5D");
return str;
}
//trim
function trim(str){
str = str.replace(/(^\s*)|(\s*$)/g,"");
return str;
}
function calculateFontSizeFitsInWidth(pDefaultFontSize, pMaxWidth, pFontFamily, pStringToTest){
var testSpan = document.createElement("span");
testSpan.style.position = "absolute";
testSpan.style.left = "9999px";
testSpan.style.top = "9999px";
testSpan.style.whiteSpace = "nowrap";
document.body.appendChild(testSpan);
if(pFontFamily){
testSpan.style.fontFamily = pFontFamily;
}
testSpan.innerText = pStringToTest;
var curFontSize = pDefaultFontSize;
var isFitNow = false;
while(!isFitNow){
testSpan.style.fontSize = curFontSize + "px";
if(testSpan.offsetWidth > pMaxWidth){
curFontSize--;
}else{
isFitNow = true;
}
}
document.body.removeChild(testSpan);
return curFontSize;
}
function objectToJSONString(pObject){
if(!pObject){return "";}
var isArray = (pObject.join && pObject.pop && pObject.push && pObject.reverse && pObject.shift && pObject.slice && pObject.splice);
var results = [];
for(var i in pObject){
var value = pObject[i];
if((typeof value) == "object"){
results.push((isArray ? "" : (i.toString() + ":")) + objectToJSONString(value));
}else if(value){
results.push((isArray ? "" : (i.toString() + ":")) + (typeof value == "string" ? "\"" + value + "\"" : value));
}else{
}
}
return (isArray ? "[" : "{") + results.join(", ") + (isArray ? "]" : "}");
}
Comments
Post a Comment