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ứ Năm, 9 tháng 8, 2018

fix lỗi không edit đc thuộc tính của ảnh trong ckeditor trên modal

Lỗi này chỉ xuất hiện khi gọi ckeditor trên modal. Cách fix là sẽ thêm 1 hàm extension cho js để force focus vào modal edit property ảnh:

$.fn.modal.Constructor.prototype.enforceFocus = function () {
    modal_this = this
    $(document).on('focusin.modal', function (e) {
        if (modal_this.$element[0] !== e.target && !modal_this.$element.has(e.target).length
        && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_select')
        && !$(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
            modal_this.$element.focus()
        }
    })
};


Read More »

Thứ Năm, 10 tháng 8, 2017

Ajax async

How do I update the returnHtml variable from within the anonymous success function?
function getPrice(productId, storeId) {
    var returnHtml = '';

    jQuery.ajax({
        url: "/includes/unit.jsp?" + params,
        cache: false,
        dataType: "html",
        success: function(html){
            returnHtml = html;
        }
    });

    return returnHtml;
}
Answer:
That's the wrong approach. The first A in AJAX is Asynchronous. That function returns before the AJAX call returns (or at least it can). So this isn't an issue of scope. It's an issue of ordering. There are only two options:
  1. Make the AJAX call synchronous (not recommended) with the async: false option (default is true); or
  2. Change your way of thinking. Instead of returning HTML from the function you need to passin a callback to be called when the AJAX call succeeds.
As an example of (2):
function findPrice(productId, storeId, callback) {
    jQuery.ajax({
        url: "/includes/unit.jsp?" + params,
        cache: false,
        dataType: "html",
        success: function(html) {
            callback(productId, storeId, html);
        }
    });
}

function receivePrice(productId, storeId, html) {
    alert("Product " + productId + " for storeId " + storeId + " received HTML " + html);
}

findPrice(23, 334, receive_price);
Read More »

Thứ Tư, 6 tháng 7, 2016

Error: XMLHttpRequest on the main thread is deprecated

To avoid this warning, do not use:
async: false
in any of your $.ajax() calls. This is the only feature of XMLHttpRequest that's deprecated.
The default is async: true, so if you never use this option at all, your code should be safe if the feature is ever really removed (it probably won't be -- it may be removed from the standards, but I'll bet browsers will continue to support it for many years).

Another reason (In my case):
- using : async: false
- Call CountAsync() (Iqueryable):
        public virtual int Count(Expression<Func<TEntity, bool>> where)
        {
              return where == null ? _dbSet.AsQueryable().CountAsync().Result :            _dbSet.Where(where).CountAsync().Result;
        }
Read More »

Thứ Năm, 22 tháng 10, 2015

CKFinder - Cách khắc phục lỗi upload ảnh bị lỗi trên hosting

Bạn phát triển website, sử dụng ckfinder để upload ảnh. Mọi thứ chạy trên localhost rất perfect, nhưng khi bạn deploy chạy trên hosting thì lại không thể upload được ảnh. Bạn nhận được lỗi: "Lỗi khi tải tệp tin".
Lỗi này là do Permision hosting của bạn, khắc phục rất đơn giản. Bạn vào hosting, đến hosting setting và tích vào checkbox "Additional write/modify permissions" -> Save.Vậy là xong :)

Read More »

Thứ Năm, 8 tháng 10, 2015

Các cách khai báo function jquery

Introduction

Choosing which way to declare a JavaScript function can be confusing for beginners as there are several different ways to declare functions using JavaScript/jQuery. I’ll try to explain the benefits of each one and how and why you might use them when writing your awesome jQuery code.

1. The basic JavaScript function

This is the simplest way to declare a function in JavaScript. Say for example, we want to write a simple function called multiply(x,y) which simply takes in two parameters x and y, does a simple x times y and returns the value. Here are a few ways you might go about doing exactly this.
function multiply(x,y) {
     return (x * y);
}
console.log(multiply(2,2));
//output: 4
If you wanted a quick function to test something then maybe that’s the only occasion you would use this. It’s not good coding and doesn’t promote code reuse.

2. JavaScript functions for get/set

If you need a private utility for getting/setting/deleting model values then you can declare a function as a variable like this. This could be useful for assigning a variable upon declaration calculated by a function.
var multiply = function(x,y) {
     return (x * y);
}
console.log(multiply(2,2));
//output: 4

//The same function but with a self execution to set the value of the variable:
var multiply = function(x,y) {
     return (x * y);
}(2,2);
console.log(multiply);
//output: 4

3. Create your own jQuery function

This is an awesome way to declare functions that can be used just like your regular jQuery functions, on your DOM elements! Rememeber jQuery.fn is just an alias for jQuery.prototype (which just saves us time when coding such jQuery.fn.init.prototype = jQuery.fn = $.fn as such).
jQuery.fn.extend({
    zigzag: function () {
        var text = $(this).text();
        var zigzagText = '';
        var toggle = true; //lower/uppper toggle
   $.each(text, function(i, nome) {
    zigzagText += (toggle) ? nome.toUpperCase() : nome.toLowerCase();
    toggle = (toggle) ? false : true;
   });
 return zigzagText;
    }
});

console.log($('#tagline').zigzag());
//output: #1 jQuErY BlOg fOr yOuR DaIlY NeWs, PlUgInS, tUtS/TiPs & cOdE SnIpPeTs.

//chained example
console.log($('#tagline').zigzag().toLowerCase());
//output: #1 jquery blog for your daily news, plugins, tuts/tips & code snippets.
Don’t forget to return the element so that you can chain jQuery functions together.

4. Extend Existing jQuery Functions

(or which either extend existing jQuery functions with extra functionality or creating your own functions that can be called using the jQuery namespace (usually, we use the $ sign to represent the jQuery namespace). In this example the $.fn.each function has been modified with custom behaviour.
(function($){

// maintain a to the existing function
var oldEachFn = $.fn.each;

$.fn.each = function() {

    // original behavior - use function.apply to preserve context
    var ret = oldEachFn.apply(this, arguments);
 
 // add custom behaviour
 try {
  // change background colour
  $(this).css({'background-color':'orange'});
  
  // add a message
  var msg = 'Danger high voltage!';
  $(this).prepend(msg);
 }
 catch(e) 
 {
  console.log(e);
 }
 
    // preserve return value (probably the jQuery object...)
    return ret;
}
})(jQuery);

5. Functions in custom namespaces

If your writing functions in a custom namespace you must declare them in this way. Extra functions can be added to the namespace you just need to add a comma after each one (except the last one!). If your unsure about namespacing see jQuery Function Namespacing in Plain English
JQUERY4U = {
 multiply: function(x,y) {
  return (x * y);
 }
}
//function call
JQUERY4U.multiply(2,2);

Conclusion

Knowing when and how to declare different types of JavaScript/jQuery functions is definitely something any good js developer should know inside out.
Read More »

Thứ Ba, 28 tháng 7, 2015

Lazy load image to speed up website

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script type="text/javascript">
        $(document).ready(function(){
            $('img.async').each(function(i, ele) {
                 $(ele).attr('src',$(ele).attr('alt'));
            });
        });
        </script> </head> <body> <img class="async" title="Гороскопы" alt="http://virtual-doctor.net/images/horoscopes.jpg" width="135" height="135"/> 
</body>
</html>

After all website component is loaded, images start loading.
Read More »