Skip to content

Instantly share code, notes, and snippets.

View kanakiyajay's full-sized avatar
💭
Reach out to my email address

Jay Kanakiya kanakiyajay

💭
Reach out to my email address
View GitHub Profile
@kanakiyajay
kanakiyajay / throttle.js
Created September 16, 2014 13:14
Throttle: Call a function only once every few milliseconds. Important for scroll events, or when a function is called multiple times.
function throttle(delay, callback) {
var previousTime = new Date().getTime();
return function() {
var newTime = new Date().getTime();
if ((newTime - previousTime) > delay) {
previousTime = newTime;
callback.apply(null, arguments);
}
};
}
@kanakiyajay
kanakiyajay / parseURL
Created October 18, 2014 13:42
Function to parseUrl and extract useful information in javascript
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
@kanakiyajay
kanakiyajay / UpdateQueryString
Created October 18, 2014 14:07
Update Query String of any url
function UpdateQueryString(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi");
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null)
return url.replace(re, '$1' + key + "=" + value + '$2$3');
else {
var hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
@kanakiyajay
kanakiyajay / addUtmParams
Created October 18, 2014 14:46
Add Utm params to each and every link of your website.
(function addUtmParams(source, medium, campaign) {
$("a").each(function () {
var $this = $(this);
var linkHref = $this.attr("href");
if (linkHref.indexOf(window.location.hostname) < 0) {
var updateLink = updateQueryString({
utm_source: source,
utm_medium: medium,
utm_campaign: campaign
}, linkHref);
@kanakiyajay
kanakiyajay / onbeforeunload.js
Created November 20, 2014 12:51
javascript snippet to alert user from not leaving
window.onbeforeunload = function (e) {
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Sure?';
}
// For Safari
return 'Sure?';
@kanakiyajay
kanakiyajay / trackingService.js
Created November 28, 2014 15:12
Tracking Google Analytics in Angular js Apps
MyApp.run(function($rootScope, $location, $routeParams, $window){
$rootScope.$on('$routeChangeSuccess', function() {
var output=$location.path()+"?";
angular.forEach($routeParams,function(value,key){
output+=key+"="+value+"&";
})
output=output.substr(0,output.length-1);
$window._gaq.push(['_trackPageView', output]);
});
})
@kanakiyajay
kanakiyajay / .htaccess
Created November 29, 2014 14:49
.htaccess files for wordpress for gzip and security.
<FilesMatch "^(wp-config\.php|wp-cache-config\.php|advanced-cache\.php|php\.ini|php5\.ini|config\.php|db\.php|db-config\.ini)">
Order Deny,Allow
Deny from all
</FilesMatch>
### If you have one or more dedicated IP addresses, uncomment the below
### from <FilesMatch to </FilesMatch then replace the IP addresess with
### your own; and completely remove any IP address line not necessary.
###
### For example, if you only have one dedicated IP address, there
@kanakiyajay
kanakiyajay / cols.css
Created December 1, 2014 05:04
Seven Equal Columns in bootstrap.
@media (min-width: 768px) {
.seven-cols .col-md-1,
.seven-cols .col-sm-1,
.seven-cols .col-lg-1 {
width: 100%;
}
}
@media (min-width: 992px) {
.seven-cols .col-md-1,
@kanakiyajay
kanakiyajay / functions.php
Created January 26, 2015 14:56
Collections of code for your wordpress functions.php file
// CUSTOM ADMIN MENU LINK FOR ALL SETTINGS
function all_settings_link() {
add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
}
add_action('admin_menu', 'all_settings_link');
add_filter( 'login_headerurl', 'namespace_login_headerurl' );
/**
* Replaces the login header logo URL
*
console.log("SHOPS: {% if shop.customer_accounts_enabled %}");