Hiển thị các bài đăng có nhãn javascript. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn javascript. Hiển thị tất cả bài đăng

Thứ Hai, 22 tháng 2, 2016

Đoạn code Format Number ( ngắt theo ngàn, triệu ... ) bằng Javascript

Viết lại từ phiên bản C# và xử lý thêm trường hợp số âm
http://www.hanhtranglaptrinh.com/2011/12/oan-code-format-number-ngat-theo-ngan.html


var insertAt = function (originalStr, index, string) {
     return originalStr.substr(0, index) + string + originalStr.substr(index);
}
var formatNumber = function (number) {
    var rs;
    var part = number.replace("-", "").split('.');
    var len = part[0].length;
    var i = parseInt(len / 3);
    if (i > 0) {
        var pos = len % 3;
        if (pos > 0) {
            part[0] = insertAt(part[0],pos, ",");
            pos += 4;
        }
        else {
            pos = 3;
        }

        for (var k = 1; k < i; k++) {
            part[0] = insertAt(part[0],pos, ",");
            pos += 4;
        }

    }
    if (number.indexOf(".") >= 0) {
        rs = part[0] + "." + part[1];
    }
    else
        rs = part[0];

    if (number.indexOf("-") == 0)
        rs = "-" + rs;

    return rs;
}

Chủ Nhật, 29 tháng 7, 2012

Sử dụng Clipboard bằng javascript

Để đưa 1 giá trị text vào clipboard:
  1. window.clipboardData.setData('Text','aaaaaaaa');  
  2.       
Để lấy giá trị text đang lưu trong clipboard:
  1. var value = window.clipboardData.getData("Text");  
  2.       
Tuy nhiên để các hàm này hoạt động thì trình duyệt người dùng phải cho phép sử dụng clipboard. Hiện giờ đa số các trình duyệt đã disable tính năng này vì lý do an ninh hay 1 vài vấn đề khác:

Problems caused by unintended clipboard access

There are several types of problems which can happen if a website is able to use your system's clipboard through javascript:
  • A website may erase the clipboard data, which may cause you to lose important data that you had copied previously into the clipboard.
  • A website may change the clipboard data, which may cause different data to be pasted later on instead of the data you intended to paste originally.
  • A website may read the clipboard data, and use them in malicious ways or send them to a third party without your knowledge.
Therefore, you should only permit the websites that you absolutely trust to be able to access the clipboard.

Thứ Hai, 18 tháng 6, 2012

Check All và UnCheck All CheckBoxList ASP.NET

Có thể xử lý check all hoặc uncheck all bên phía server hoặc client nhưng mình thích sử dụng javascript trên client hơn vì không phải postback lên server. Đặc biệt nữa là sử dụng javascript thì không phụ thuộc vào công nghệ nào cả, có thể sử dụng cho nhiều công nghệ làm web khác nhau.

Hàm js sử dụng để check all hoặc uncheck all các item trong control CheckBoxList ASP.NET
  1. <script language="javascript" type="text/javascript">  
  2.     function SetAllStateCheckBoxList(chkID, state) {  
  3.         var chk = document.getElementById(chkID);  
  4.         var items = chk.getElementsByTagName("input");  
  5.         for (var i = 0; i < items.length; i++) {  
  6.             items[i].checked = state;  
  7.         }  
  8.     }  
  9. </script>  
  10.       
Sử dụng:
  1. <input type="button" value="chọn hết" onclick="SetAllStateCheckBoxList('<%=CheckBoxList1.ClientID %>',true)" />  
  2. <input type="button" value="bỏ chọn hết" onclick="SetAllStateCheckBoxList('<%=CheckBoxList1.ClientID %>',false)" />  
  3. <asp:CheckBoxList ID="CheckBoxList1" runat="server">  
  4. </asp:CheckBoxList>  
  5.      

Chủ Nhật, 10 tháng 6, 2012

Sử dụng Cookie bằng javascript

Hôm nay mình làm chức năng đăng nhập bằng javascript, cần sử dụng cookie để lưu trữ thông tin đăng nhập cho phần nhớ mật khẩu. Đây là đoạn code javascript để sử dụng cookie mình tham khảo trên w3schools:
  1. function getCookie(c_name) {  
  2.   
  3.     var i, x, y, ARRcookies = document.cookie.split(";");  
  4.   
  5.     for (i = 0; i < ARRcookies.length; i++) {  
  6.   
  7.         x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));  
  8.   
  9.         y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);  
  10.   
  11.         x = x.replace(/^\s+|\s+$/g, "");  
  12.   
  13.         if (x == c_name) {  
  14.   
  15.             return unescape(y);  
  16.   
  17.         }  
  18.   
  19.     }  
  20.   
  21. }  
  22.   
  23. function setCookie(c_name, value, exdays) {  
  24.   
  25.     var exdate = new Date();  
  26.   
  27.     exdate.setDate(exdate.getDate() + exdays);  
  28.   
  29.     var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());  
  30.   
  31.     document.cookie = c_name + "=" + c_value;  
  32.   
  33. }  
  34.       
Xem thêm Sử dụng Cookie trong ASP.NET