Search found 4 matches for javascript

 22.01.16 19:28

Code:
name: source caching
created: 17:10 - 01/22/2016
updated: 17:10 - 01/22/2016


Giới thiệu:
Khi bạn sử dụng hệ thống lưu tải tệp này, trang của bạn chỉ tải dữ liệu lần đầu, những lần gọi sau điều được lấy từ bộ nhớ đệm, tiết kiệm băng thông, thời gian tải trang và dữ liệu tối đa.
Sử dụng mã javascript tuần nên không cần chạy nền jQuery.
Chỉ hỗ trợ trình duyệt có HTML5.

Lưu ý:
- Tùy vào việc cập nhật tệp nguồn, hãy tùy chỉnh thời gian nhớ đúng với lịch cập nhật của bạn.
- Đối với việc sử dụng trong diễn đàn, bạn các quản trị nên tạo mới (refresh) mỗi lần thành viên đăng nhập hoặc lần truy cập đầu tiên.
- Hạn chế việc sử dụng cho tệp có tính thay đổi cao, có hệ khắc phục bằng việc giảm thời gian nhớ.

Kịch bản:
Code:
/*
name: source caching - lamhieu (@lh)
created: 17:10 - 01/22/2016
updated: 17:10 - 01/22/2016
*/

var _lh = {};
_lh.sourcecache = {
 key: 'lamhieu',
 request: {
 create: function () {
 var objectXHR = false;
 if (window.XMLHttpRequest) {
 var objectXHR = new XMLHttpRequest();
 }else if (window.ActiveXObject) {
 try {
 var objectXHR = new ActiveXObject("Msxml2.XMLHTTP.6.0");
 } catch (e) {
 try {
 var objectXHR = new ActiveXObject("Msxml2.XMLHTTP.3.0");
 } catch (e) {
 //.
 }
 }
 }
 return objectXHR;
 },
 get: function (url, callback) {
 var xhr = _lh.sourcecache.request.create();
 xhr.onreadystatechange = function() {
   if (xhr.readyState == 4) {
   callback({'return': true, 'status': xhr.status, 'response': (xhr.responseText ? xhr.responseText : null)});
   }
   };
 xhr.open("GET", url, true);
 xhr.send();
 return true;
 }
 },
 exec: function (type, content, callback) {
 var type = typeof type == 'string' ? type : null,
 content = typeof content == 'string' ? content : null,
 callback = typeof callback == 'function' ? callback : function () {};
 if (type == 'css' || type == 'stylesheet') {
 var cssTag = document.createElement('style');
 cssTag.type = 'text/css';
 if (cssTag.styleSheet) {
 cssTag.styleSheet.cssText = content;
 }else {
 cssTag.appendChild(document.createTextNode(content));
 }
 document.getElementsByTagName("head")[0].appendChild(csscssTag);
 callback(true);
 return true;
 }else if (type == 'js' || type == 'javascript') {
 eval(content);
 callback(true);
 return true;
 }
 },
 load: function (type, url, timeout, callback) {
 var type = typeof type == 'string' ? type : nullm
 url = typeof url == 'string' ? url : null,
 timeout = typeof timeout == 'number' ? timeout : null,
 callback = typeof callback == 'function' ? callback : function () {};
 if (type == null || url == null || timeout == null) {
 callback(false);
 return false;
 }
 var timeNow = Math.floor(Date.now() / 1000);
 var cachedListObj = _lh.sourcecache.cache.each();
 cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
 if (typeof cachedListObj['sourcecache'][url] == 'object') {
 var sourceObj = cachedListObj['sourcecache'][url];
 var sourceType = typeof sourceObj['type'] == 'string' ? sourceObj['type'] : null,
 sourceContent = typeof sourceObj['content'] == 'string' ? sourceObj['content'] : null,
 sourceExpired = typeof sourceObj['added'] == 'number' && typeof sourceObj['timeout'] == 'number' ? sourceObj['added'] + sourceObj['timeout'] : 0;
 if (sourceType == type) {
 if (sourceExpired >= timeNow) {
 var isExce = _lh.sourcecache.exec(type, sourceContent);
 callback({'return': true, 'loaded': isExce, 'content': sourceContent, 'added': false, 'cached': true, 'timeleft': ((timeNow + timeout) - sourceExpired)});
 return true;
 }
 }
 }
 return _lh.sourcecache.request.get(url, function (returnRequest) {
 if (typeof returnRequest == 'object') {
 if (typeof returnRequest['return'] == 'boolean' && returnRequest['return'] == true && typeof returnRequest['response'] == 'string') {
 var sourceContent = returnRequest['response'];
 var isExce = _lh.sourcecache.exec(type, sourceContent);
 var isAdded = _lh.sourcecache.cache.push({'url': url, 'type': type, 'content': sourceContent, 'timeout': timeout});
 callback({'return': true, 'loaded': isExce, 'content': sourceContent, 'added': isAdded, 'cached': false, 'timeleft': timeout});
 }
 }
 });
 },
 checkif: {
 json: function (str) {
 try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
 },
 supported: function () {
 if (typeof localStorage == 'object') {
 return true;
 }else {
 return false;
 }
 },
 },
 storage: {
 write: function (data, callback) {
 var data = typeof data == 'object' ? data : {},
 callback = typeof callback == 'function' ? callback : function () {};
 data['sourcecache'] = typeof data['sourcecache'] == 'object' ? data['sourcecache'] : {};
 if (typeof localStorage != 'object') {
 callback(false);
 return false;
 }
 localStorage[_lh.sourcecache.key] = JSON.stringify(data);
 callback(true);
 return true;
 },
 get: function (callback) {
 callback = typeof callback == 'function' ? callback : function () {};
 var contentStorage = null;
 if (typeof localStorage === 'object' && typeof localStorage[_lh.sourcecache.key] === 'string') {
 if (_lh.sourcecache.checkif.json(localStorage[_lh.sourcecache.key])) {
 contentStorage = JSON.parse(localStorage[_lh.sourcecache.key]);
 }
 }
 callback(contentStorage);
 return contentStorage;
 }
 },
 cache: {
 push: function (object, callback) {
 var object = typeof object == 'object' ? object : {},
 callback = typeof callback == 'function' ? callback : function () {};
 var sourceUrl = typeof object['url'] == 'string' ? object['url'] : null,
 sourceType = typeof object['type'] == 'string' ? object['type'] : null,
 sourceContent = typeof object['content'] == 'string' ? object['content'] : null,
 sourceTimeout = typeof object['timeout'] == 'number' ? object['timeout'] : null;
 if (sourceType == null || sourceType == null || sourceType == null) {
 callback(false);
 return false;
 }
 var timeNow = Math.floor(Date.now() / 1000);
 var cachedListObj = _lh.sourcecache.cache.each();
 cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
 cachedListObj['sourcecache'][sourceUrl] = {'type': sourceType, 'content': sourceContent, 'added': timeNow, 'timeout': sourceTimeout};
 return _lh.sourcecache.storage.write(cachedListObj, function (isPushed) {
 callback(isPushed);
 return isPushed;
 });
 },
 each: function (callback) {
 var callback = typeof callback == 'function' ? callback : function () {};
 var cachedObj = _lh.sourcecache.storage.get();
 if (cachedObj == null) {
 cachedObj = {};
 }
 callback(cachedObj);
 return cachedObj;
 },
 clean: function (callback) {
 var callback = typeof callback == 'function' ? callback : function () {};
 var timeNow = Math.floor(Date.now() / 1000);
 var cachedListObj = _lh.sourcecache.cache.each();
 cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
 for (var sourceUrl in cachedListObj['sourcecache']) {
 var sourceObj = cachedListObj['sourcecache'][sourceUrl];
 if (sourceObj['added'] + sourceObj['timeout'] < timeNow) {
 delete cachedListObj['sourcecache'][sourceUrl];
 }
 }
 return _lh.sourcecache.storage.write(cachedListObj, function (isPushed) {
 callback(isPushed);
 return isPushed;
 });
 },
 refresh: function (callback) {
 var callback = typeof callback == 'function' ? callback : function () {};
 var timeNow = Math.floor(Date.now() / 1000);
 var cachedListObj = _lh.sourcecache.cache.each();
 cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
 return _lh.sourcecache.storage.write(cachedListObj, function (isPushed) {
 callback(isPushed);
 return isPushed;
 });
 }
 }
};

Thêm vào bất kỳ nơi vào bạn muốn, chèn trong thẻ hoặc trích dẫn ngoài đường dẫn riêng.
(Mẹo: thêm vào thẻ <head> để được tải)

Sử dụng:
Code:
var fileType = 'js', // kiểu tệp nguồn: css, js,..
 fileUrl = '...', // đường dẫn đến tệp nguồn
 fileTimeout = 12 * 60 * 60; // thời gian lưu trữ tạm, tín bằng giây. ví dụ: 12 * 60 * 60 là 12 giờ
 fileCallback = function (fileLoadedObj) {
      console.log(fileLoadedObj); // giá tri trả về
}; // hàm gọi lại sau khi hoàn tất
_lh.sourcecache.load(fileType, fileUrl, (12 * 60 * 60), fileTimeout);


Ví dụ:
Code:
_lh.sourcecache.load('js', 'https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.1.4\/jquery.min.js', (24* 60 * 60));

đoạn mã trên sử dụng để tải jQuery từ Google libs, lưu trữ trong 1 ngày. Vì chưa đủ ngày nên mình thêm dấu \ vào mỗi dấu / để không bị chặn.

Kiểm tra khả dụng:
Code:
if (_lh.sourcecache.checkif.supported()) {
 // trình duyệt hỗ trợ.
}else {
 // trình duyệt không hỗ trợ.
}

Tags: [You must be registered and logged in to see this link.]

[Hỏi đáp] Avatar ajax post

Diễn đàn: Hỏi đápTrả lời: 6Lượt xem: 1199

 19.08.15 5:39

hi is possible send new avatar in ajax form

Code:
$.post('/profile?mode=editprofile&page_profil=avatars', {
    tid: ,
    enctype: ,
    coppa: "",
    username: ,
    email: ,
    page_profil: "avatars",
    mode: "editprofile",
    agreed: "true",
    user_id: _userdata.user_id,
    current_email: ,
    avatarurl:,

})


help me please
Tags: #javascript

[Hỏi đáp] Hi i write from Mexico

Diễn đàn: Hỏi đápTrả lời: 7Lượt xem: 1529

 24.07.15 1:07

Hi my friends i read your forum since 2 years ago, i'ts a excellent site, i use this thread several times to write my own codes

https://devs.forumvi.com/t520-gist-mot-so-cach-dung-ajax-trong-forumotion

but now is gone :( can you help me?

i want work in this zone

   /contact

how is the $.post elements to send?


sorry my english is ugly and i'm not speak Vietnamese, but google tanslator help very well :)


Tags: #javascript

 26.07.14 0:04

Cập nhật mod Topic prefixes cũ của Việt K. Mod này sẽ tạo ra một danh mục các tiền tố (prefix) cho người dùng lựa chọn khi họ đăng bài viết.

Topics tagged under javascript on DEVs forumvi 3GS5LhP

Thay code prefix của ViệtK bằng code này:
Code:
var prefixes = ["Thông báo", "Tài liệu", "Bàn luận", "Chia sẻ", "Giải trí", "Truyện", "Tin tức", "Video - Clip", "Âm nhạc", "Bầu chọn", "Kiến thức", "Kĩ năng", "Tranh luận"]; //danh sách các prefix
var _pm = false; //true: cho phép dùng prefix khi gửi tin nhắn
/*!
 * Topic prefixes - Copyright © 2011 by Viet K - chinhphuc.info
 * Fix by Zzbaivong - devs.forumvi.com
 */
$(function() {
    var chk = false;
    if (_pm) chk = /\privmsg/.test(location.href);
    if (/\/post/.test(location.href) || chk) {
        var $select = $("<select>", {
            id: "prefix",
            style: "margin-top: 2px; margin-right:5px; height: 21px",
            size: 1,
            html: '<option value="">(Chọn tiền tố)</option>'
        });
        $.each(prefixes, function(i, val) {
            $("<option>", {
                value: val,
                text: val
            }).appendTo($select);
        });
        var $subject = $("input[name='subject']");
        $select.insertBefore("input[name='subject']");
        $subject.width($subject.width() - $select.width() - 5);
        var testPrefix = new RegExp("^\\[(" + prefixes.join("|") + ")\\]");
        var title = $subject.val();
        if (testPrefix.test(title)) {
            var prefix = title.match(/^\[([^\[\]]+)\]\s*(.*)/);
            $select.find("option[value='" + prefix[1] + "']").attr("selected", true);
            console.log($select.find("option[value='" + prefix[1] + "']"));
            $subject.val(prefix[2]);
        }
        $("input[name='post']").click(function() {
            if ($subject.val().trim() !== "" && $select.val() !== "") $subject.val("[" + $select.val() + "] " + $subject.val())
        });
    }
});

Tags: #mod #javascript #jquery #prefix


Back to top