<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>
<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
]]>
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');
]]>
// 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'
// convert string to snake case String.prototype.toSnakeCase = function () { return this.replace(/(.)([A-Z])/g, "$1_$2").toLowerCase(); };
Usage:
'YourName'.toSnakeCase(); Output: 'your_name'
// 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
// 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
// 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
// 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()]; };
// 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 = { 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
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
Array.prototype.random = function () { return this[Math.floor((Math.random()*this.length))]; }
Usage:
[1, 2, 3, 4].random();
Storage.prototype.setObject = function(key, value) { this.setItem(key, JSON.stringify(value)); }
Usage:
localStorage.setObject('numbers', [1, 2, 3, 4]);
Storage.prototype.getObject = function(key) { var value = this.getItem(key); return value && JSON.parse(value); }
Usage:
localStorage.getObject('numbers');
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); });
]]>