⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.js

📁 ajax开发实例
💻 JS
📖 第 1 页 / 共 4 页
字号:
/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//** * Declare an object to which we can add real functions. */if (window['dojo']) dojo.provide('dwr.util');if (typeof window['dwr'] == 'undefined') window.dwr = {};if (typeof dwr['util'] == 'undefined') dwr.util = {};//if (typeof window['DWRUtil'] == 'undefined') window.DWRUtil = dwr.util;/** @private The flag we use to decide if we should escape html */dwr.util._escapeHtml = true;/** * Set the global escapeHtml flag */dwr.util.setEscapeHtml = function(escapeHtml) {  dwr.util._escapeHtml = escapeHtml;};/** @private Work out from an options list and global settings if we should be esccaping */dwr.util._shouldEscapeHtml = function(options) {  if (options && options.escapeHtml != null) {    return options.escapeHtml;  }  return dwr.util._escapeHtml;};/** * Return a string with &, < and > replaced with their entities * @see TODO */dwr.util.escapeHtml = function(original) {  return original.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/'/g,'&#039;');};/** * Replace common XML entities with characters (see dwr.util.escapeHtml()) * @see TODO */dwr.util.unescapeHtml = function(original) {  return original.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&#039;/g,"'").replace(/&amp;/g,'&');};/** * Replace characters dangerous for XSS reasons with visually similar characters * @see TODO */dwr.util.replaceXmlCharacters = function(original) {  original = original.replace("&", "+");  original = original.replace("<", "\u2039");  original = original.replace(">", "\u203A");  original = original.replace("\'", "\u2018");  original = original.replace("\"", "\u201C");  return original;};/** * Return true iff the input string contains any XSS dangerous characters * @see TODO */dwr.util.containsXssRiskyCharacters = function(original) {  return (original.indexOf('&') != -1    || original.indexOf('<') != -1    || original.indexOf('>') != -1    || original.indexOf('\'') != -1    || original.indexOf('\"') != -1);};/** * Enables you to react to return being pressed in an input * @see http://getahead.org/dwr/browser/util/selectrange */dwr.util.onReturn = function(event, action) {  if (!event) event = window.event;  if (event && event.keyCode && event.keyCode == 13) action();};/** * Select a specific range in a text box. Useful for 'google suggest' type functions. * @see http://getahead.org/dwr/browser/util/selectrange */dwr.util.selectRange = function(ele, start, end) {  ele = dwr.util._getElementById(ele, "selectRange()");  if (ele == null) return;  if (ele.setSelectionRange) {    ele.setSelectionRange(start, end);  }  else if (ele.createTextRange) {    var range = ele.createTextRange();    range.moveStart("character", start);    range.moveEnd("character", end - ele.value.length);    range.select();  }  ele.focus();};/** * Find the element in the current HTML document with the given id or ids * @see http://getahead.org/dwr/browser/util/$ */dwr.util.byId = function() {  var elems = [];  for (var i = 0; i < arguments.length; i++) {    var idOrElem = arguments[i];    var elem;    if (typeof idOrElem == 'string') {      var elem = document.getElementById(idOrElem);      // Workaround for IE and Opera that may return element based on name      // (note use of elem.attributes.id.value due to IE bug for elem.getAttribute)       if (document.all && elem && elem.attributes.id.value != idOrElem) {        elem = null;        var maybeElems = document.all[idOrElem];        if (maybeElems.tagName) maybeElems = [maybeElems];        for (var j = 0; j < maybeElems.length; j++) {          if (maybeElems[j].attributes.id.value == idOrElem) {            elem = maybeElems[j];            break;          }        }      }    }    else {      elem = idOrElem;    }    if (arguments.length == 1) {      return elem;    }    elems.push(elem);  }  return elems;}/** * Alias $ to dwr.util.byId * @see http://getahead.org/dwr/browser/util/$ */if (window['$'] == null) {  window['$'] = dwr.util.byId;}/** * This function pretty-prints simple data or whole object graphs, f ex as an aid in debugging. * @see http://getahead.org/dwr/browser/util/todescriptivestring */dwr.util.toDescriptiveString = function(data, showLevels, options) {  if (showLevels === undefined) showLevels = 1;  var opt = {};  if (dwr.util._isObject(options)) opt = options;  var defaultoptions = {    escapeHtml:false,    baseIndent: "",    childIndent: "\u00A0\u00A0",    lineTerminator: "\n",    oneLineMaxItems: 5,    shortStringMaxLength: 13,    propertyNameMaxLength: 30   };  for (var p in defaultoptions) {    if (!(p in opt)) {      opt[p] = defaultoptions[p];    }  }  var skipDomProperties = {    document:true, ownerDocument:true,    all:true,    parentElement:true, parentNode:true, offsetParent:true,    children:true, firstChild:true, lastChild:true,    previousSibling:true, nextSibling:true,    innerHTML:true, outerHTML:true,    innerText:true, outerText:true, textContent:true,    attributes:true,    style:true, currentStyle:true, runtimeStyle:true,    parentTextEdit:true  };    function recursive(data, showLevels, indentDepth, options) {    var reply = "";    try {      // string      if (dwr.util._isString(data)) {        var str = data;        if (showLevels == 0 && str.length > options.shortStringMaxLength)          str = str.substring(0, options.shortStringMaxLength-3) + "...";        if (options.escapeHtml) {          // Do the escape separately for every line as escapeHtml() on some           // browsers (IE) will strip line breaks and we want to preserve them          var lines = str.split("\n");          for (var i = 0; i < lines.length; i++) lines[i] = dwr.util.escapeHtml(lines[i]);          str = lines.join("\n");        }        if (showLevels == 0) { // Short format          str = str.replace(/\n|\r|\t/g, function(ch) {            switch (ch) {              case "\n": return "\\n";              case "\r": return "";              case "\t": return "\\t";            }          });        }        else { // Long format          str = str.replace(/\n|\r|\t/g, function(ch) {            switch (ch) {              case "\n": return options.lineTerminator + indent(indentDepth+1, options);              case "\r": return "";              case "\t": return "\\t";            }          });        }        reply = '"' + str + '"';      }            // function      else if (dwr.util._isFunction(data)) {        reply = "function";      }          // Array      else if (dwr.util._isArrayLike(data)) {        if (showLevels == 0) { // Short format (don't show items)          if (data.length > 0)            reply = "[...]";          else            reply = "[]";        }        else { // Long format (show items)          var strarr = [];          strarr.push("[");          var count = 0;          for (var i = 0; i < data.length; i++) {            if (!(i in data) && data != "[object NodeList]") continue;            var itemvalue = data[i];            if (count > 0) strarr.push(", ");            if (showLevels == 1) { // One-line format              if (count == options.oneLineMaxItems) {                strarr.push("...");                break;              }            }            else { // Multi-line format              strarr.push(options.lineTerminator + indent(indentDepth+1, options));            }            if (i != count) {              strarr.push(i);              strarr.push(":");            }            strarr.push(recursive(itemvalue, showLevels-1, indentDepth+1, options));            count++;          }          if (showLevels > 1) strarr.push(options.lineTerminator + indent(indentDepth, options));          strarr.push("]");          reply = strarr.join("");        }      }            // Objects except Date      else if (dwr.util._isObject(data) && !dwr.util._isDate(data)) {        if (showLevels == 0) { // Short format (don't show properties)          reply = dwr.util._detailedTypeOf(data);        }        else { // Long format (show properties)          var strarr = [];          if (dwr.util._detailedTypeOf(data) != "Object") {            strarr.push(dwr.util._detailedTypeOf(data));            if (typeof data.valueOf() != "object") {              strarr.push(":");              strarr.push(recursive(data.valueOf(), 1, indentDepth, options));            }            strarr.push(" ");          }          strarr.push("{");          var isDomObject = dwr.util._isHTMLElement(data);           var count = 0;          for (var prop in data) {            var propvalue = data[prop];            if (isDomObject) {              if (!propvalue) continue;              if (typeof propvalue == "function") continue;              if (skipDomProperties[prop]) continue;              if (prop.toUpperCase() == prop) continue;            }            if (count > 0) strarr.push(", ");            if (showLevels == 1) { // One-line format              if (count == options.oneLineMaxItems) {                strarr.push("...");                break;              }            }            else { // Multi-line format              strarr.push(options.lineTerminator + indent(indentDepth+1, options));            }            strarr.push(prop.length > options.propertyNameMaxLength ? prop.substring(0, options.propertyNameMaxLength-3) + "..." : prop);            strarr.push(":");            strarr.push(recursive(propvalue, showLevels-1, indentDepth+1, options));            count++;          }          if (showLevels > 1 && count > 0) strarr.push(options.lineTerminator + indent(indentDepth, options));          strarr.push("}");          reply = strarr.join("");        }      }        // undefined, null, number, boolean, Date      else {        reply = "" + data;      }        return reply;    }    catch(err) {      return (err.message ? err.message : ""+err);    }  }  function indent(count, options) {    var strarr = [];    strarr.push(options.baseIndent);    for (var i=0; i<count; i++) {      strarr.push(options.childIndent);    }    return strarr.join("");  };    return recursive(data, showLevels, 0, opt);};/** * Setup a GMail style loading message. * @see http://getahead.org/dwr/browser/util/useloadingmessage */dwr.util.useLoadingMessage = function(message) {  var loadingMessage;  if (message) loadingMessage = message;  else loadingMessage = "Loading";  dwr.engine.setPreHook(function() {    var disabledZone = dwr.util.byId('disabledZone');    if (!disabledZone) {      disabledZone = document.createElement('div');      disabledZone.setAttribute('id', 'disabledZone');      disabledZone.style.position = "absolute";      disabledZone.style.zIndex = "1000";      disabledZone.style.left = "0px";      disabledZone.style.top = "0px";      disabledZone.style.width = "100%";      disabledZone.style.height = "100%";      // IE need a background color to block click. Use an invisible background.      if (window.ActiveXObject) {        disabledZone.style.background = "white";        disabledZone.style.filter = "alpha(opacity=0)";      }      document.body.appendChild(disabledZone);      var messageZone = document.createElement('div');      messageZone.setAttribute('id', 'messageZone');      messageZone.style.position = "absolute";      messageZone.style.top = "0px";      messageZone.style.right = "0px";      messageZone.style.background = "red";      messageZone.style.color = "white";      messageZone.style.fontFamily = "Arial,Helvetica,sans-serif";      messageZone.style.padding = "4px";      document.body.appendChild(messageZone);      var text = document.createTextNode(loadingMessage);      messageZone.appendChild(text);      dwr.util._disabledZoneUseCount = 1;    }    else {      dwr.util.byId('messageZone').innerHTML = loadingMessage;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -