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.     }) ()

製造出純html的原始碼線上工具(Online Syntex Highlighter)

quickhighlighter

VB.net 動態變更使用者 switch user dynamically

文章參考: 如何在 ASP.NET 應用程式中實作模擬

VB.net
  1. Public Class Personate
  2.     Private Shared LOGON32_LOGON_INTERACTIVE As Integer = 2
  3.     Private Shared LOGON32_PROVIDER_DEFAULT As Integer = 0
  4.     Private Shared ImpersonationContext As WindowsImpersonationContext
  5.  
  6.     Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
  7.                             ByVal lpszDomain As String, _
  8.                             ByVal lpszPassword As String, _
  9.                             ByVal dwLogonType As Integer, _
  10.                             ByVal dwLogonProvider As Integer, _
  11.                             ByRef phToken As IntPtr) As Integer
  12.  
  13.     Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
  14.                             ByVal ExistingTokenHandle As IntPtr, _
  15.                             ByVal ImpersonationLevel As Integer, _
  16.                             ByRef DuplicateTokenHandle As IntPtr) As Integer
  17.  
  18.     Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
  19.     Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
  20.  
  21.     Public Shared Function ImpersonateValidUser(ByVal userName As String, ByVal domain As String, ByVal password As String) As Boolean
  22.  
  23.         Dim tempWindowsIdentity As WindowsIdentity
  24.         Dim token As IntPtr = IntPtr.Zero
  25.         Dim tokenDuplicate As IntPtr = IntPtr.Zero
  26.         ImpersonateValidUser = False
  27.  
  28.         If RevertToSelf() Then
  29.             If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
  30.                 If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
  31.                     tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
  32.                     ImpersonationContext = tempWindowsIdentity.Impersonate()
  33.                     If Not ImpersonationContext Is Nothing Then
  34.                         ImpersonateValidUser = True
  35.                     End If
  36.                 End If
  37.             End If
  38.         End If
  39.         If Not tokenDuplicate.Equals(IntPtr.Zero) Then
  40.             CloseHandle(tokenDuplicate)
  41.         End If
  42.         If Not token.Equals(IntPtr.Zero) Then
  43.             CloseHandle(token)
  44.         End If
  45.     End Function
  46.  
  47.     Public Shared Sub UndoImpersonation()
  48.         ImpersonationContext.Undo()
  49.     End Sub
  50. End Class

VB.net Convert to JSON

VB.net
  1. Public Class JSONConvert
  2.     Public Shared Function DataTable2NameValueCollection(ByVal dt As DataTable) As NameValueCollection
  3.         Dim columnlist As New List(Of String)()
  4.         For Each column As DataColumn In dt.Columns
  5.             columnlist.Add(column.ColumnName)
  6.         Next
  7.  
  8.         Dim rowlist As New List(Of String)()
  9.         For Each row As DataRow In dt.Rows
  10.             Dim itemlist As New List(Of String)()
  11.             For Each column As DataColumn In dt.Columns
  12.                 If column.DataType.Equals(GetType(Int32)) Or column.DataType.Equals(GetType(Double)) Then
  13.                     itemlist.Add(row(column.ColumnName))
  14.                 Else
  15.                     itemlist.Add(String.Format("""{0}""", row(column.ColumnName)))
  16.                 End If
  17.             Next
  18.             rowlist.Add(String.Format("[{0}]", String.Join(", ", itemlist.ToArray())))
  19.         Next
  20.  
  21.         Dim nv As New NameValueCollection()
  22.         nv.Add("columns", Array2JSONArray(columnlist.ToArray()))
  23.         nv.Add("rows", Array2JSONArray(rowlist.ToArray()))
  24.         Return nv
  25.     End Function
  26.  
  27.     Public Shared Function DataTable2JSONObject(ByVal dt As DataTable) As String
  28.         Dim nv As NameValueCollection = DataTable2NameValueCollection(dt)
  29.         Return NameValueCollection2JSONObjectArray(nv)
  30.     End Function
  31.  
  32.     Public Shared Function NameValueCollection2JSONObjectArray(ByVal nv As System.Collections.Specialized.NameValueCollection) As String
  33.         Dim list As New List(Of String)()
  34.         For Each name As String In nv.AllKeys
  35.             If Regex.IsMatch(nv(name), "^(.*(\[|\]|{|})+.*|\d+)$") Then
  36.                 list.Add(String.Format("""{0}"": {1}", name, nv(name)))
  37.             Else
  38.                 list.Add(String.Format("""{0}"": ""{1}""", name, nv(name)))
  39.             End If
  40.         Next
  41.         Return String.Format("{{{0}}}", String.Join(", ", list.ToArray()))
  42.     End Function
  43.  
  44.     Public Shared Function Array2JSONArray(ByVal arr As Array) As String
  45.         If Regex.IsMatch(String.Join(", ", CType(arr, String())), "^(.*(\[|\]|{|})+.*|\d+)$") Then
  46.             Return ObjectArray2JSONArray(arr)
  47.         Else
  48.             Return StringArray2JSONArray(arr)
  49.         End If
  50.     End Function
  51.  
  52.     Public Shared Function StringArray2JSONArray(ByVal arr() As String) As String
  53.         If arr.Length = 0 Then
  54.             Return ("[]")
  55.         Else
  56.             Return String.Format("[""{0}""]", String.Join(""", """, arr))
  57.         End If
  58.     End Function
  59.  
  60.     Public Shared Function ObjectArray2JSONArray(ByVal arr As Array) As String
  61.         Return String.Format("[{0}]", String.Join(", ", CType(arr, String())))
  62.     End Function
  63. End Class

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. });