Snippet Javascript

With snippet Javascript, you can free your mind

  1. Check Browser by Javascript

    Use navigator object for this and in that you can use userAgent property

    if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 10) {
      // Firefox version 10.0 and up
      // Allow
    } else if (navigator.userAgent.indexOf('Chrome') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Chrome') + 7).split(' ')[0]) >= 15) {
      // Chrome version 15 and up
      // Allow
    } else if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Version') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Version') + 8).split(' ')[0]) >= 5){
      // Safari version 5 and up
      // Allow
    } else {
      // Block
    }
    
  2. Select All Checkboxs Are Not Checked

    $("input:checkbox:unchecked").click();
    $("input:checkbox:unchecked").prop("checked", "checked");
    
  3. Rounding Decimals

    The most common solutions for rounding to a decimal place is to either use Number.prototype.toFixed(), or multiply the float by some power of 10 in order to leverage Math.round(). Both of these work, except sometimes a decimal of 5 is rounded down instead of up.

    Number((1.005).toFixed(2)); // 1 instead of 1.01
    Math.round(1.005*100)/100; // 1 instead of 1.01
    

    A Better Solution

    The rounding problem can be avoided by using numbers represented in exponential notation:

    Number(Math.round(1.005+'e2')+'e-2'); // 1.01
    

    And to abstract that into something more usable:

    function round(value, decimals) {
      return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
    }
    round(1.005, 2); // 1.01
    
  4. Shuffle Array

    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex ;
      // While there remain elements to shuffle...
      while (0 !== currentIndex) {
        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
      }
      return array;
    }
    
  5. Check Scrollbar Visible

    (function($) {
      $.fn.hasScrollBar = function() {
        return this.get(0).scrollHeight > this.height();
      }
    })(jQuery);
    
    $(document).ready(function() {
      $('#div1').hasScrollBar(); // returns true if vertical scrollbar visible, false otherwise
    });
    

    Notes

    • If you have horizontal scrollbar that causes vertical scrollbar to appear, use clientHeight instead (this.get(0).clientHeight)
    • If you have padding, use innerHeight instead (this.innerHeight())

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections