- String.prototype.encodeHTML = function() { var d=document.createElement("div");(d.textContent)?(d.textContent=this):d.innerText=this;return d.innerHTML };
- String.prototype.decodeHTML = function() { var d=document.createElement("div");d.innerHTML=this;return d.innerText||d.textContent };
2011/12/13
html encode/decode using javascript
2011/04/09
使用javascript產生guid
參考來源: http://note19.com/2007/05/27/javascript-guid-generator/
javascript
- function guid() {
- function S4() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- }
- return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
- }
html javascript 小時鐘 digit clock
就像這樣:
html javascript
- <span id="__current_datetime__">
- <script type="text/javascript">
- (function () {
- var target = "__current_datetime__";
- var padLeft = function(str) {
- str = "00" + str;
- return str.substring(str.length-2)
- };
- var getCurrentDateTime = function() {
- var d = new Date();
- return d.getFullYear() + "年"
- + padLeft(d.getMonth()+1) + "月"
- + padLeft(d.getDate()) + "日 "
- + padLeft(d.getHours()) + ":"
- + padLeft(d.getMinutes()) + ":"
- + padLeft(d.getSeconds());
- };
- var updateCurrentDateTime = function(id) {
- document.getElementById(id).innerHTML = getCurrentDateTime();
- };
- updateCurrentDateTime(target);
- setInterval(function() {
- updateCurrentDateTime(target);
- }, 99);
- }) ()
2011/04/08
prototype.js extend Ajax.Queue
javascript
- var GUID = function () {
- function S4() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- }
- function guid() {
- return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
- }
- return guid();
- };
javascript
- Ajax.Queue = Class.create({
- initialize: function () {
- this.id = GUID();
- this.active = false;
- this.completed = true;
- this.queue = [];
- this.stopped = false;
- Ajax.Queues.push(this);
- },
- add: function (url, options) {
- this.queue.push({ "url": url, "options": options });
- return this.queue.length - 1;
- },
- enqueue: function (url, options) {
- var index = this.add(url, options);
- if (!this.active) this.start();
- return index;
- },
- clear: function () {
- this.queue = [];
- },
- start: function () {
- if (this.queue.length == 0) {
- this.active = false;
- this.completed = true;
- }
- else if (this.stopped) {
- this.stopped = false;
- this.clear();
- this.active = false;
- this.completed = true;
- }
- else {
- this.active = true;
- var params = this.queue.shift();
- params.options = params.options || {};
- var handlers = {
- "Success": function (transport) {
- if (typeof console == "object") console.log("success");
- this.start();
- },
- "Failure": function (transport) {
- if (typeof console == "object") console.log("failure");
- this.active = false;
- },
- "Complete": function (transport) {
- if (typeof console == "object") console.log("complete");
- this.active = false;
- },
- "Exception": function (transport, e) {
- if (typeof console == "object") console.log("exception");
- this.active = false;
- this.exception = e;
- }
- };
- var that = this;
- Object.keys(handlers).each(function (event) {
- var eventName = "on" + event;
- var originalHandler = params.options[eventName];
- var handler = handlers[event];
- params.options[eventName] = (function () {
- if (Object.isFunction(originalHandler)) {
- originalHandler.apply(this, arguments);
- }
- handler.apply(this, arguments);
- }).bind(that);
- });
- this.completed = false;
- new Ajax.Request(params.url, params.options);
- }
- },
- stop: function () {
- this.stopped = true;
- }
- });
- Ajax.Queues = [];
- Ajax.Q = new Ajax.Queue();
usage
- Ajax.Q.enqueue("url", {
- onSuccess: function (transport) {
- try {
- alert(transport.responseText);
- } catch (e) { if(typeof(console) == "object") console.log(e) }
- },
- onFailure: function (ajax) {
- if(typeof(console) == "object") console.log(e);
- }
- });
2008/01/18
Extend String
Javascript
- Object.extend(String.prototype, {
- padLeft: function(token, length) {
- var value = this;
- if(value.length < length) {
- token = token.toString().charAt(0);
- var cnt = length - value.length;
- for(var i = 0;i<cnt;i++) {
- value = token + value;
- }
- }
- return value;
- },
- padRight: function(token, length) {
- var value = this;
- if(value.length < length) {
- token = token.toString().charAt(0);
- var cnt = length - value.length;
- for(var i = 0;i<cnt;i++) {
- value += token;
- }
- }
- return value;
- }
- });
訂閱:
文章 (Atom)