Skip to content

Instantly share code, notes, and snippets.

@skamenetskiy
Last active August 29, 2015 14:17
Show Gist options
  • Save skamenetskiy/398a7512960e827b4ab2 to your computer and use it in GitHub Desktop.
Save skamenetskiy/398a7512960e827b4ab2 to your computer and use it in GitHub Desktop.
A simple library for adding a searchObject parameter to global location object.
/**
* A simple library for adding a searchObject parameter to global location object.
* The script supports IE7+ and the normal browsers
* @copyright Semen Kamenetskiy <skamenetskiy@live.com>
* @link https://gist.github.com/skamenetskiy/398a7512960e827b4ab2
*/
(function (Window, History, Location, _objectName, _prefix, _states) {
'use strict';
/**
* Parse the query string
* @returns {_parse|SearchObject}
* @private
*/
function _parse() {
var a, pair, pairs, query = Location.search.substring(1);
for (a in this) {
if (this.hasOwnProperty(a)) {
delete this[a]
}
}
if (query.length > 0) {
pairs = query.split('&');
for (a = 0; a < pairs.length; a++) {
pair = pairs[a].split('=');
this[pair[0]] = decodeURIComponent(pair[1])
}
}
return this
}
/**
* Main SearchObject constructor
* @constructor
* @type {SearchObject}
*/
function SearchObject() {
/**
* Current SearchObject instance
* @type {SearchObject}
*/
var $this = this;
/**
* State change events handler
* @private
*/
function _onChangeState() {
_parse.call($this)
}
// Check if history api is supported
if (History.pushState) {
/**
* Attach state change events
*/
_states.forEach(function (state) {
var _lowerState = state.toLowerCase();
if (History[state]) {
// Save a copy of the original state event
History[_prefix + state] = History[state];
// Override original state event
History[state] = function () {
History[_prefix + state].apply(History, arguments);
Window.dispatchEvent(new CustomEvent(_lowerState));
}
}
Window.addEventListener(_lowerState, _onChangeState)
})
}
}
/**
* Initialize SearchObject within location
* @type {_parse|SearchObject}
*/
Location[_objectName] = _parse.call(new SearchObject)
})(
// Global object
window,
// History object
history,
// Location object
location,
// Object name
'searchObject',
// Override method prefix
'__',
// Override states
[
'pushState',
'replaceState',
'popState'
]
);
/*
Semen Kamenetskiy <skamenetskiy@live.com>
@link https://gist.github.com/skamenetskiy/398a7512960e827b4ab2
*/
(function(e,d,f,k,g,l){function h(){var c,a,b;a=f.search.substring(1);for(c in this)this.hasOwnProperty(c)&&delete this[c];if(0<a.length)for(b=a.split("&"),c=0;c<b.length;c++)a=b[c].split("="),this[a[0]]=decodeURIComponent(a[1]);return this}f[k]=h.call(new function(){function c(){h.call(a)}var a=this;d.pushState&&l.forEach(function(b){var a=b.toLowerCase();d[b]&&(d[g+b]=d[b],d[b]=function(){d[g+b].apply(d,arguments);e.dispatchEvent(new CustomEvent(a))});e.addEventListener(a,c)})})})(window,history,
location,"searchObject","__",["pushState","replaceState","popState"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment