Skip to content

Instantly share code, notes, and snippets.

function handlePathChange() {
actionCreator.handlePathChange({newPath:location.pathname + location.search});
}
// setup event listener for back/forward buttons
var eventInfo = window.addEventListener ? ['addEventListener', ''] : ['attachEvent', 'on'];
window[eventInfo[0]](eventInfo[1] + 'popstate', handlePathChange, false);
@gilbox
gilbox / trackFn.js
Last active August 29, 2015 14:03
Function tracking for console debugging
/**
* Track a function attached to an object.
* For example to track: myObject.myFunc()
* trackFn(myObject, 'myFunc');
*
* You can also track multiple functions at once:
* trackFn(myObject, ['myFunc1','myFunc2','myFunc3']);
*
* optionally set st=true to get a stack trace
* optionally you can supply a msg argument
svc = function allServices(mod, r) {
var inj = angular.element(document).injector().get;
if (!r) r = {};
angular.forEach(angular.module(mod).requires, function(m) {allServices(m,r)});
angular.forEach(angular.module(mod)._invokeQueue, function(a) {
try { r[a[2][0]] = inj(a[2][0]); } catch (e) {}
});
return r;
}();
@gilbox
gilbox / getURLParameter.js
Created December 13, 2013 23:34
Parse GET parameters from the current page's URL
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
@gilbox
gilbox / gist:7683414
Created November 27, 2013 21:21
Get a jQuery selector for any element on a page in Chrome (or any WebKit browser). [assuming jQuery has been loaded on the page]
// right-click any element and choose copy xpath, it will look something like this:
//*[@id="list-results"]/div[2]/div[1]/div/ol/li[5]/div/a
var MY_XPATH = '//*[@id="list-results"]/div[2]/div[1]/div/ol/li[5]/div/a'
// then do this:
$($x(MY_XPATH))
@gilbox
gilbox / stack-trace.js
Created November 13, 2013 16:45
A one-liner for a doing a quick stack trace in JavaScript
console.log((new Error('STACK TRACE')).stack);
@gilbox
gilbox / encodeURIObject.js
Last active December 28, 2015 03:18
Encodes a JavaScript object (with key/values) into a URI (ie: key1=value1&key2=value2...)
function encodeURIObject(o) {
var a = [];
for (k in o) {
a.push(encodeURIComponent(k) + '=' + encodeURIComponent(o[k]))
}
return a.join('&');
}
// test:
var uriStringTest = encodeURIObject({