顯示具有 javascript 標籤的文章。 顯示所有文章
顯示具有 javascript 標籤的文章。 顯示所有文章

2011/12/13

html encode/decode using javascript

  1. String.prototype.encodeHTML = function() { var d=document.createElement("div");(d.textContent)?(d.textContent=this):d.innerText=this;return d.innerHTML };
  2. String.prototype.decodeHTML = function() { var d=document.createElement("div");d.innerHTML=this;return d.innerText||d.textContent };

2011/04/09

使用javascript產生guid

參考來源: http://note19.com/2007/05/27/javascript-guid-generator/
javascript
  1. function guid() {
  2.     function S4() {
  3.         return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  4.     }
  5.     return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  6. }

html javascript 小時鐘 digit clock


就像這樣:
html javascript
  1. <span id="__current_datetime__">
  2. <script type="text/javascript">
  3.     (function () {
  4.         var target = "__current_datetime__";
  5.         var padLeft = function(str) {
  6.             str = "00" + str;
  7.             return str.substring(str.length-2)
  8.         };
  9.         var getCurrentDateTime = function() {
  10.             var d = new Date();
  11.             return d.getFullYear() + "&#24180;"
  12.                 + padLeft(d.getMonth()+1) + "&#26376;"
  13.                 + padLeft(d.getDate()) + "&#26085; "
  14.                 + padLeft(d.getHours()) + ":"
  15.                 + padLeft(d.getMinutes()) + ":"
  16.                 + padLeft(d.getSeconds());
  17.         };
  18.         var updateCurrentDateTime = function(id) {
  19.             document.getElementById(id).innerHTML = getCurrentDateTime();
  20.         };
  21.         updateCurrentDateTime(target);
  22.         setInterval(function() {
  23.             updateCurrentDateTime(target);
  24.         }, 99);
  25.     }) ()

2011/04/08

prototype.js extend Ajax.Queue

javascript
  1. var GUID = function () {
  2.     function S4() {
  3.         return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  4.     }
  5.     function guid() {
  6.         return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  7.     }
  8.     return guid();
  9. };


javascript
  1. Ajax.Queue = Class.create({
  2.     initialize: function () {
  3.         this.id = GUID();
  4.         this.active = false;
  5.         this.completed = true;
  6.         this.queue = [];
  7.         this.stopped = false;
  8.         Ajax.Queues.push(this);
  9.     },
  10.     add: function (url, options) {
  11.         this.queue.push({ "url": url, "options": options });
  12.         return this.queue.length - 1;
  13.     },
  14.     enqueue: function (url, options) {
  15.         var index = this.add(url, options);
  16.         if (!this.active) this.start();
  17.         return index;
  18.     },
  19.     clear: function () {
  20.         this.queue = [];
  21.     },
  22.     start: function () {
  23.         if (this.queue.length == 0) {
  24.             this.active = false;
  25.             this.completed = true;
  26.         }
  27.         else if (this.stopped) {
  28.             this.stopped = false;
  29.             this.clear();
  30.             this.active = false;
  31.             this.completed = true;
  32.         }
  33.         else {
  34.             this.active = true;
  35.             var params = this.queue.shift();
  36.             params.options = params.options || {};
  37.             var handlers = {
  38.                 "Success": function (transport) {
  39.                     if (typeof console == "object") console.log("success");
  40.                     this.start();
  41.                 },
  42.                 "Failure": function (transport) {
  43.                     if (typeof console == "object") console.log("failure");
  44.                     this.active = false;
  45.                 },
  46.                 "Complete": function (transport) {
  47.                     if (typeof console == "object") console.log("complete");
  48.                     this.active = false;
  49.                 },
  50.                 "Exception": function (transport, e) {
  51.                     if (typeof console == "object") console.log("exception");
  52.                     this.active = false;
  53.                     this.exception = e;
  54.                 }
  55.             };
  56.             var that = this;
  57.             Object.keys(handlers).each(function (event) {
  58.                 var eventName = "on" + event;
  59.                 var originalHandler = params.options[eventName];
  60.                 var handler = handlers[event];
  61.                 params.options[eventName] = (function () {
  62.                     if (Object.isFunction(originalHandler)) {
  63.                         originalHandler.apply(this, arguments);
  64.                     }
  65.                     handler.apply(this, arguments);
  66.                 }).bind(that);
  67.             });
  68.             this.completed = false;
  69.             new Ajax.Request(params.url, params.options);
  70.         }
  71.     },
  72.     stop: function () {
  73.         this.stopped = true;
  74.     }
  75. });
  76. Ajax.Queues = [];
  77. Ajax.Q = new Ajax.Queue();


usage
  1. Ajax.Q.enqueue("url", {
  2.     onSuccess: function (transport) {
  3.         try {
  4.             alert(transport.responseText);
  5.         } catch (e) { if(typeof(console) == "object") console.log(e) }
  6.     },
  7.     onFailure: function (ajax) {
  8.         if(typeof(console) == "object") console.log(e);
  9.     }
  10. });

2008/01/18

Extend String

Javascript
  1. Object.extend(String.prototype, {
  2.     padLeft: function(token, length) {
  3.         var value = this;
  4.         if(value.length &lt; length) {
  5.             token = token.toString().charAt(0);
  6.             var cnt = length - value.length;
  7.             for(var i = 0;i&lt;cnt;i++) {
  8.                 value = token + value;
  9.             }
  10.         }
  11.         return value;
  12.     },
  13.     padRight: function(token, length) {
  14.         var value = this;
  15.         if(value.length &lt; length) {
  16.             token = token.toString().charAt(0);
  17.             var cnt = length - value.length;
  18.             for(var i = 0;i&lt;cnt;i++) {
  19.                 value += token;
  20.             }
  21.         }
  22.         return value;
  23.     }
  24. });