Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property POMO_FileReader::$is_overloaded is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/streams.php on line 26

Deprecated: Creation of dynamic property POMO_FileReader::$_pos is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/streams.php on line 29

Deprecated: Creation of dynamic property POMO_FileReader::$_f is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/streams.php on line 160

Deprecated: Creation of dynamic property MO::$_gettext_select_plural_form is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/translations.php on line 292

Deprecated: Creation of dynamic property POMO_FileReader::$is_overloaded is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/streams.php on line 26

Deprecated: Creation of dynamic property POMO_FileReader::$_pos is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/streams.php on line 29

Deprecated: Creation of dynamic property POMO_FileReader::$_f is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/streams.php on line 160

Deprecated: Creation of dynamic property MO::$_gettext_select_plural_form is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/pomo/translations.php on line 292

Deprecated: Creation of dynamic property WP_Block_Type::$skip_inner_blocks is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-block-type.php on line 391

Deprecated: Creation of dynamic property WP_Block_Type::$skip_inner_blocks is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-block-type.php on line 391

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home3/achieve/public_html/knowledgebase/wp-includes/class-wp-term-query.php on line 1118

Warning: Cannot modify header information - headers already sent by (output started at /home3/achieve/public_html/knowledgebase/wp-includes/wp-db.php:760) in /home3/achieve/public_html/knowledgebase/wp-includes/feed-rss2.php on line 8
JavaScript – Knowledge http://knowledge.achieveee.com Wed, 07 Jun 2017 09:55:47 +0000 en-US hourly 1 https://wordpress.org/?v=6.0.7 http://knowledge.achieveee.com/wp-content/uploads/2016/05/cropped-favicon-32x32.png JavaScript – Knowledge http://knowledge.achieveee.com 32 32 WebFont – Load Google Fonts & TypeKit asynchronously http://knowledge.achieveee.com/knowledge_base/webfont-load-google-fonts-typekit-asynchronously/ http://knowledge.achieveee.com/knowledge_base/webfont-load-google-fonts-typekit-asynchronously/#respond Wed, 07 Jun 2017 09:55:47 +0000 http://knowledge.achieveee.com/?post_type=knowledge_base&p=2520 Google & TypeKit collaborated to load web fonts asynchronously, here are the methods:

  1. Include Google Host Library directly:
    <script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
    <script>
      WebFont.load({
        google: {
          families: ['Droid Sans', 'Droid Serif']
        }
      });
    </script>
  2. Include Google Host Library asynchronously:
    <script type="text/javascript">
      WebFontConfig={
        google:{families:["Lato:400,700,300,100:latin"]}
      };
      
      (function(){
        var wf = document.createElement("script");
        wf.src = ("https:" == document.location.protocol ? "https":"http") + 
                 "://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js";
        wf.type="text/javascript";
        wf.async="true";
        var t = document.getElementsByTagName("script")[0];
        t.parentNode.insertBefore(wf, t)
      })();
    </script>

 

You can also download Google WebFont Loader locally and call WebFont.load()  function
For more details, check: https://github.com/typekit/webfontloader

]]>
http://knowledge.achieveee.com/knowledge_base/webfont-load-google-fonts-typekit-asynchronously/feed/ 0
Calculate distance between center of two HTML objects http://knowledge.achieveee.com/knowledge_base/calculate-distance-between-center-of-two-html-objects/ http://knowledge.achieveee.com/knowledge_base/calculate-distance-between-center-of-two-html-objects/#respond Thu, 08 Dec 2016 13:05:15 +0000 http://knowledge.achieveee.com/?post_type=knowledge_base&p=2500 // get center position of an html element function getPositionAtCenter(element) { var data = element.getBoundingClientRect(); return { x: data.left + data.width / 2, y: data.top + data.height / 2 }; } // get distance between two elements function getDistanceBetweenElements(a, b) { var aPosition = getPositionAtCenter(a); var bPosition = getPositionAtCenter(b); return Math.sqrt(Math.pow(aPosition.x - bPosition.x, 2) + Math.pow(aPosition.y - bPosition.y, 2)); } function get_distance() { var distance = getDistanceBetweenElements( document.getElementById(object1), document.getElementById(object2) ); return distance; }

 

]]>
http://knowledge.achieveee.com/knowledge_base/calculate-distance-between-center-of-two-html-objects/feed/ 0
Bind/Unbind Mouse Move, Direction Keys and Touch Swipe events from JavaScript http://knowledge.achieveee.com/knowledge_base/bindunbind-mouse-move-direction-keys-and-touch-swipe-events-from-javascript/ http://knowledge.achieveee.com/knowledge_base/bindunbind-mouse-move-direction-keys-and-touch-swipe-events-from-javascript/#respond Thu, 08 Dec 2016 12:58:24 +0000 http://knowledge.achieveee.com/?post_type=knowledge_base&p=2495 1. Touch Swipe Events for Touch Devices

var xDown = null;
var yDown = null;
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);

function handleTouchStart(evt) {
    xDown = evt.touches[0].clientX;
    yDown = evt.touches[0].clientY;
};

function handleTouchMove(evt) {
    if (!xDown || !yDown) {
        return;
    }

    var xUp = evt.touches[0].clientX;
    var yUp = evt.touches[0].clientY;

    var xDiff = xDown - xUp;
    var yDiff = yDown - yUp;

    if (Math.abs(xDiff) > Math.abs(yDiff)) {
        if (xDiff > 0) {
        // swipe right direction
        }
        else {
        // swipe left direction
        }
    }
    else {
        if (yDiff > 0) {
        // swipe down direction
        }
        else {
        // swipe up direction
        }
    }

    /* reset values */
    xDown = null;
    yDown = null;
};

Unbind event:

$('body').unbind('touchmove');

2. Direction Arrow Keys Binding

// set key binding for direction arrows
$(document).keydown(function(e) {
    switch(e.which) {
        case 37:
        // left direction
        break;

        case 38:
        // up direction
        break;

        case 39:
        // right direction
        break;

        case 40:
        // down direction
        break;

        default: return; // exit this handler for other keys
    }
    e.preventDefault(); // prevent the default action (scroll / move caret)
});

Unbind event:

$(document).unbind('keydown');

3. Mouse move direction events

var last_position = {};

$(document).on('mousemove', function (event) {
    //check to make sure there is data to compare against
    if (typeof(last_position.x) != 'undefined') {

        //get the change from last position to this position
        var deltaX = last_position.x - event.clientX,
        deltaY = last_position.y - event.clientY;

        //check which direction had the highest amplitude and then figure out direction by checking if the value is greater or less than zero
        if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX > 0) {
            // mouse move left
        }
        else if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX < 0) {
            // mouse move right
        }
        else if (Math.abs(deltaY) > Math.abs(deltaX) && deltaY > 0) {
            // mouse move up
        }
        else if (Math.abs(deltaY) > Math.abs(deltaX) && deltaY < 0) {
            // mouse move down
        }
    }

    //set the new last position to the current for next time
    last_position = {
        x : event.clientX,
        y : event.clientY
    };
});

Unbind event:

$(document).off('mousemove');

 

]]>
http://knowledge.achieveee.com/knowledge_base/bindunbind-mouse-move-direction-keys-and-touch-swipe-events-from-javascript/feed/ 0
Useful JavaScript Prototypes and methods http://knowledge.achieveee.com/knowledge_base/useful-javascript-prototypes-and-methods/ http://knowledge.achieveee.com/knowledge_base/useful-javascript-prototypes-and-methods/#respond Fri, 06 May 2016 14:23:28 +0000 http://achieveee.com/knowledgebase/?post_type=knowledge_base&p=2449 Here are some useful JavaScript prototypes and methods for various datatypes to extend your application functionalities:

  1. String

    • Proper Case (Convert string to Proper/Title Case)
      // convert string to proper case
      String.prototype.toProperCase = function () {
          return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
      };
      

      Usage:

      'your name'.toProperCase();
      Output: 'Your Name'
    • Snake Case (Convert studly case ‘YourName’ to ‘your_name’)
      // convert string to snake case
      String.prototype.toSnakeCase = function () {
          return this.replace(/(.)([A-Z])/g, "$1_$2").toLowerCase();
      };

      Usage:

      'YourName'.toSnakeCase();
      Output: 'your_name'
    • Is Date (Check if string is a Date)
      // check if string is a valid date
      String.prototype.isDate = function () {
          var dateFormat;
      
          if (toString.call(this) === '[object Date]') {
              return true;
          }
          if (typeof this.replace === 'function') {
              this.replace(/^\s+|\s+$/gm, '');
          }
      
          dateFormat = /(^\d{1,4}[\.|\\/|-]\d{1,2}[\.|\\/|-]\d{1,4})(\s*(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d\s*[ap]m)?$/;
      
          if (dateFormat.test(this)) {
              return !!new Date(this).getTime();
          }
      
          return false;
      };

      Usage:

      '12-03-1990'.isDate();
      Output: true
    • Is Time (Check if string is a Time)
      // check if string is a time
      String.prototype.isTime = function () {
          var isValid = /^([0-1]?[0-9]|2[0-3]):([0-5][0-9])(:[0-5][0-9])?$/.test(this);
          return isValid;
      };

      Usage:

      '14:36'.isTime();
      Output: true
    • Is DateTime (Check if string is DateTime, to make it working, copy the above Date & Time Prototype)
      // check if string is a time
      String.prototype.isDateTime = function () {
          var date = this.split(" ");
      
          if (date[0].isDate() && date[1].isTime()) {
              return true;
          }
      
          return false;
      };

      Usage:

      '20-10-1990 14:36'.isDateTime();
      Output: true
  2. Date (Get Long and Short Month Names with Multilingual Support)

    • Month Long Name
      // getting month long name from Date object
      Date.prototype.getMonthName = function(lang) {
          lang = lang && (lang in Date.locale) ? lang : 'en';
          return Date.locale[lang].month_names[this.getMonth()];
      };
    • Month Short Name
      // getting month short name from Date object
      Date.prototype.getMonthNameShort = function(lang) {
          lang = lang && (lang in Date.locale) ? lang : 'en';
          return Date.locale[lang].month_names_short[this.getMonth()];
      };
    • Date Locale(Multilingual Support)
      Date.locale = {
          en: {
              month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
              month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
          }
      };

      Usage:

      var current_month = new Date().getMonthName(lang);
      var current_short_name = new Date().getMonthNameShort(lang);

      lang parameter is optional

  3. Array

    • Contains (Check if given element exists in Array, similar to jQuery inArray)
      Array.prototype.contains = function(obj) {
          var i = this.length;
          while (i--) {
              if (this[i] === obj) {
                  return true;
              }
          }
          return false;
      }

      Usage:

      ['Red', 'Yellow', 'Green', 'Blue'].contains('Green');
      Output: true
    • Random (Pick a random element from array)
      Array.prototype.random = function () {
          return this[Math.floor((Math.random()*this.length))];
      }

      Usage:

      [1, 2, 3, 4].random();
  4.  Storage

    • Set Object in LocalStorage/SessionStorage
      Storage.prototype.setObject = function(key, value) {
          this.setItem(key, JSON.stringify(value));
      }

      Usage:

      localStorage.setObject('numbers', [1, 2, 3, 4]);
    • Get Object from LocalStorage/Session
      Storage.prototype.getObject = function(key) {
          var value = this.getItem(key);
          return value && JSON.parse(value);
      }

      Usage:

      localStorage.getObject('numbers');
]]>
http://knowledge.achieveee.com/knowledge_base/useful-javascript-prototypes-and-methods/feed/ 0
JavaScript popup window with close event on button click http://knowledge.achieveee.com/knowledge_base/javascript-popup-window-with-close-event-on-button-click/ http://knowledge.achieveee.com/knowledge_base/javascript-popup-window-with-close-event-on-button-click/#respond Fri, 06 May 2016 12:53:43 +0000 http://achieveee.com/knowledgebase/?post_type=knowledge_base&p=2444 window.open() is a method to open a new browser window.

Javascript code to open window popup center aligned to page with close event on button click:

$(btn-container).on("click", function() {
    var w = 620;
    var h = 350;
    var win = window.open('http://achieveee.com', 'Achieveee', 'resizable,toolbar=0,location=0,scrollbars=1,menubar=0,width='+w+',height='+h+',top='+(screen.height-h)/2+',left='+(screen.width-w)/2+'');

    var timer = setInterval(function() {
        if (win.closed) {
            clearInterval(timer);
            alert('closed');
        }
    }, 1000);
});

 

]]>
http://knowledge.achieveee.com/knowledge_base/javascript-popup-window-with-close-event-on-button-click/feed/ 0