项目中的js

var Size = function(width, height) {
 this.width = width * 1.0 || 0.0;
 this.height = height * 1.0 || 0.0;
};
Size.prototype = {
 toString : function() {
  return this.width + ' ' + this.height;
 },
 equals : function(other) {
  return (this.width == other.width && this.height == other.width);
 },
 isEmpty : function() {
  return this.width <= 0 || this.height <= 0;
 }
};
Size.parse = function(string) {
 var parts = string.split(/\s+/);
 return new Size(parts[0], parts[1]);
};

var Pos = function(left, top) {
 this.left = left * 1.0 || 0.0;
 this.top = top * 1.0 || 0.0;
};
Pos.prototype = {
 toString : function() {
  return this.left + ' ' + this.top;
 },
 equals : function(other) {
  return (this.left == other.left && this.top == other.top);
 },
 isEmpty : function() {
  return this.left <= 0 || this.top <= 0;
 }
}
Pos.parse = function(str) {
 var parts = str.split(/\s+/);
 return new Pos(parts[0], parts[1]);
};
//判断窗口是否最小化(跨浏览器)
function isMinStatus() {
 var isMin = false;
 if (window.outerWidth != undefined) {
  isMin = window.outerWidth <= 160 && window.outerHeight <= 27;
 }else {
  isMin = window.screenTop < -30000 && window.screenLeft < -30000;
 }
 return isMin;
}
function startsWith(str, prefix,bool){
 if(bool){
  str = str.toLowerCase();
  prefix = prefix.toLowerCase(); 
 }
 return str.indexOf(prefix) == 0;
}
function endsWith(str, suffix,bool){
 if(bool){
  str = str.toLowerCase();
  prefix = prefix.toLowerCase(); 
 }
 return str.lastIndexOf(suffix) == (str.length - suffix.length);
}
function isSet(obj) {
 if(typeof obj === 'undefined') return false;
 if(obj == null) return false;
 return true;
}

function isFunction(obj) {
 return typeof obj === 'function';
}

function isArray(obj) {
 return typeof obj === 'array';
}

function html2text( html ) {
    var trans = {
        '&' : '&amp;',
        '<' : '&lt;',
        '>' : '&gt;',
        '\n': '<br/>',
        ' ' : '&nbsp',
        '"' : '&quot;',
        "'" : '&#x27;'
    };
    return (html + '').replace(/[&<>\"\'\n\ ]/g, function(c) { return trans[c]; });
}

function getElementsByClass(className, parentElement) {

 var elems = (parentElement || document.body).getElementsByTagName("*");
 var result = [];
 for (var i = 0; i < elems.length; i++) {
  var j = elems[i];
  if (hasClass(j, className)) {
   result.push(j);
  }
 }
 /*if(result.length === 0) {
  alert('element not found for class name : ' + className);
 }*/
 return result;
}

function getChildElements(elem) {
 var result = [];
 var nodes = elem.childNodes;
 for(var i=0;i<nodes.length;i++) {
  var j=nodes[i];
  if(j.nodeType != 3 && j.nodeName != "#text") {
   result.push(j);
  }
 }
 return result;
}

function hasClass(elem, className) {
 if(!elem || !elem.className || elem.className.length == 0) return false;
 return new RegExp("(^|\\s)" + className + "(\\s|$)").test(elem.className);
}

function addClass(elem, className) {
 if(hasClass(elem, className)) return;
 elem.className += elem.className == '' ? className : ' ' + className;
}

function removeClass(elem, className) {
 if(!hasClass(elem, className)) {
  return;
 }else{
  var reg = new RegExp("(^|\\s)" + className + "(\\s|$)");
  var res = reg.exec(elem.className)[0];
  elem.className = trim(elem.className.replace(reg,' '));
  return res;
 }
}

function getRootElement() {
 return document.compatMode == "CSS1Compat" && document.documentElement || document.body;
}

function getLeft(elem) {
 return elem.offsetParent ? (elem.offsetLeft + getLeft(elem.offsetParent))
   : elem.offsetLeft;
}

function getTop(elem) {
 return elem.offsetParent ? (elem.offsetTop + getTop(elem.offsetParent))
   : elem.offsetTop;
}

function getElementPos(elem) {
 if (!isVisual(elem)) return new Pos(0, 0);
 var x = getLeft(elem);
 var y = getTop(elem) - getRootElement().scrollTop;
 return new Pos(x, y);
}

function setElementPos(elem, pos) {
 if(pos.top > 0) elem.style.top = pos.top + 'px';
 if(pos.left > 0) elem.style.left = pos.left + 'px';
}

function getElementStyle(elem) {
    if (document && document.defaultView && document.defaultView.getComputedStyle) {
        return document.defaultView.getComputedStyle( elem, null );
    } else if (elem.currentStyle) {
        return elem.currentStyle;
    }
}

function show(elem) {
 elem.style.display = 'block';
}

function hide(elem) {
 elem.style.display = 'none';
}

function isVisual(elem) {
 var style = getElementStyle(elem);
 return (style && style.display != 'none' && style.display != null);
}

function getElementSize(elem) {
 var style = getElementStyle(elem);
 if(style != null) {
  var borderWidth = parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
  borderWidth = borderWidth > 0 ? borderWidth : 0;
  var borderHeight = parseInt(style.borderTopWidth) + parseInt(style.borderBottomWidth);
  borderHeight = borderHeight > 0 ? borderHeight : 0;
 }
 
    return new Size(elem.offsetWidth, elem.offsetHeight);
}

function getPaddingSize(elem) {
 var style = getElementStyle(elem);
 if(style != null) {
  var paddingWidth = parseInt(style.paddingLeft) + parseInt(style.paddingRight);
  paddingWidth = paddingWidth > 0 ? paddingWidth : 0;
  var paddingHeight = parseInt(style.paddingTop) + parseInt(style.paddingBottom);
  paddingHeight = paddingHeight > 0 ? paddingHeight : 0;
    
  return new Size(paddingWidth, paddingHeight);
 }
 return new Size(0, 0);
}

function getBorderSize(elem) {
 var style = getElementStyle(elem);
 if(style != null) {
  var borderWidth = parseInt(style.borderLeftWidth) + parseInt(style.borderRightWidth);
  borderWidth = borderWidth > 0 ? borderWidth : 0;
  var borderHeight = parseInt(style.borderTopWidth) + parseInt(style.borderBottomWidth);
  borderHeight = borderHeight > 0 ? borderHeight : 0;
    
  return new Size(borderHeight, borderHeight);
 }
 return new Size(0, 0);
}

function setElementSize(elem, size) {
 elem['size'] = size;
 
 var psize = getPaddingSize(elem);
 var bsize = getBorderSize(elem);
 size.width -= psize.width + bsize.width;
 size.height -= psize.height + bsize.height;
 if(size.width > 0) elem.style.width = size.width + 'px';
 if(size.height > 0) elem.style.height = size.height + 'px';
 
 elem['innerSize'] = size;
 return size;
}

function setElementPos(elem, pos) {
 if(pos.left > 0) elem.style.left = pos.left + 'px';
 if(pos.top > 0) elem.style.top = pos.top + 'px';
}

function getInnerSize(elem) {
 var psize = getPaddingSize(elem);
 var bsize = getBorderSize(elem);
 var size = getElementSize(elem);
 
 size.width = size.width - psize.width - bsize.height;
 size.height = size.height - psize.height - bsize.height;
 return size;
}
/*
function setCaretPosition(elem, pos) {
 // 设置光标位置函数
 if (elem.setSelectionRange) {//非IE浏览器
  elem.setSelectionRange(pos, pos);
  elem.focus();
 } else if (elem.createTextRange) {//IE浏览器
  var range = elem.createTextRange();
  range.moveEnd('character', pos);
  range.moveStart('character', pos);
  range.collapse(true);
  range.select();
 }
}*/
Array.prototype.indexOf = function(elt)
{
    var len = this.length;
   
    var from = Number(arguments[1]) || 0;
    from = (from < 0)
        ? Math.ceil(from)
        : Math.floor(from);
    if (from < 0)
        from += len;

    for (; from < len; from++)
    {
        if (from in this && this[from] === elt)
            return from;
    }
    return -1;
}

Array.prototype.remove = function(elt)
{
    for(var i=0,n=0;i<this.length;i++)
    {
        if(this[i] != elt)
        {
            this[n++]=this[i]
        }
    }
    if(this.length > 0) this.length -= 1;
}

Array.prototype.removeAt = function(idx)
{
 if(idx < 0 || idx > this.length) return;
 
    for(var i=0,n=0;i<this.length;i++)
    {
        if(i != idx)
        {
            this[n++]=this[i]
        }
    }
    this.length -= 1;
}

function bind(elem, type, handler, bubble) {
 var callbackHandler = function(e) {
  var ev = window.event || e;
  var elem = ev.srcElement || ev.target;
  if(bubble == true) {
   if (window.event) {
    ev.cancelBubble = true;
   } else {
    ev.stopPropagation();
   }
  }
  return handler(elem, ev);
 };
 if(elem.addEventListener) {
  elem.addEventListener(type, callbackHandler, false);
 } else {
  elem.attachEvent('on' + type, callbackHandler);
 }
}

function unbind(elem, type, handler) {
 elem.removeEventListener ? elem.removeEventListener(type, handler, false)
   : elem.detachEvent('on' + type, elem[type + handler]);
}

function fire(elem, type) {
 if (document.createEvent) {
  var ev = document.createEvent("HTMLEvents");
  ev.initEvent(type, true, true );
  return !elem.dispatchEvent(ev);
 } else if( document.createEventObject ) {
  var ev = document.createEventObject();
  return elem.fireEvent('on' + type, ev);
 }
}

// 完全拷贝 arg1 Object arg2 Copy....
function extend() {
    var target = arguments[0] || {}, i = 1, length = arguments.length, options;
    for ( ; i < length; i++ ) {
        if ((options = arguments[i]) != null ) {
            for ( var name in options ) {
                var copy = options[ name ];
                if ( copy !== undefined ) {
                 if(isSet(target[ name ])) {
                  if(!isSet(target['base'])) target['base'] = function(){};
                  target['base'][name] = target[ name ];
                 }
                 target[ name ] = copy;
                }
            }
        }
    }
    return target;
}

function trim( text ) {
 if(text == null || text.length == 0) return '';
 if(typeof text !== 'string') text = new String(text);
 return text.replace(/^\s+|\s+$/g,"");
}

C = 0;
function safeAlert(text) {
 if(C++ < 10) alert(text);
}

function parseParameters() {
 var result = {};
 
 var ms = window.location.href.match(/\?(.+)/);
 if(ms.length != 2) return result;

 var kvs = ms[1].split('&');
 for(var i=0;i<kvs.length;i++) {
  var kv = kvs[i].split('=');
  result[kv[0]] = kv[1];
 }
 return result;
}

function getParam(key,uri){
 return (new RegExp("([^(&|\?)]*)" + key + "=([^(&|#)]*)").test(uri+"#")) ? RegExp.$2 : null;
}
function getDecodeURIComponent(text){
 try{
  text = decodeURIComponent(text);
 }catch(e){
  text = "未知";
 }
 return text;
}
window.Core = Core = {};

var ua = navigator.userAgent;
var idx = ua.indexOf("MSIE");
if(idx > 0 || document.all) {
    Core.browser = 'I';
    var version = ua.substring(idx+2,2);
    if(parseInt(version) <= 6)
        Core.browser = 'I|6';
} else if(ua.indexOf("Netscape") >= 0)  Core.browser = 'N';
else if(ua.indexOf("Opera") >= 0) Core.browser = 'O';
else if(ua.indexOf("Gecko") >= 0) Core.browser = 'F';
else if(ua.indexOf("WebKit") >= 0) Core.browser = 'W';
else Core.browser = '';

Core.resizeTimer = null;
Core.resize = function() {
 if(Core.resizeTimer != null) return;
 Core.resizeTimer = setTimeout(function(elem, e){
  var size = new Size(parseInt(document.documentElement.clientWidth),parseInt(document.documentElement.clientHeight));
  for(var i=0,j=document.body.childNodes.length;i<j;i++) {
   var elem = document.body.childNodes[i];
   if(typeof elem.resizeTo == 'function') {
    elem.resizeTo(size);
   }
  }
  Core.resizeTimer = null;
 },100);
}
Core.getParam = getParam;

Core.Debug = true;
Core.ANALYSERURI = Core.Debug?"http://192.168.12.48:8004":"http://analyser.shangjitong.3158.cn";
Core.CDNURI = Core.Debug?"http://192.168.12.48:8004":"http://cdn.shangjitong.3158.cn";
Core.HTURI = Core.Debug?"http://192.168.12.48:8010":"http://shangjitong.3158.cn/webim";//后台uri

//Core.ANALYSERURI = Core.Debug?"http://192.168.12.56:8081":"http://analyser.webim.3jia5.com";
//Core.CDNURI = Core.Debug?"http://192.168.12.56:8081":"http://webimcdn.3jia5.com";
//Core.HTURI = Core.Debug?"http://192.168.12.56:8888":"http://im.3jia5.com/webim";//后台uri
Core.isExternal = false;
initExternal = function(){
 Core.isExternal = true;
}
Core.isLogin = false;

//window.onerror = function(sMessage, sUrl, sLine) {
// jsonp((__web_im_server?__web_im_server:"") +"/im/error?&m="+ sMessage + "&u=" + sUrl + "&l=" + sLine);
// return true;
//};


插入到某个节点后边

insertAfter:function(newElement, targetElement){
        var parent = targetElement.parentNode;
        if(parent.lastChild == targetElement){
            parent.appendChild(newElement);
        }else{
            parent.insertBefore(newElement, targetElement.nextSibling);
        }
    }



你可能感兴趣的:(项目中的js)