Skip to content

Commit

Permalink
added possibility to use urls with query string
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Mar 5, 2012
1 parent 9a9e22a commit 7f3e62d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Source/History.handleInitialState.js
Expand Up @@ -19,7 +19,7 @@ provides: History.handleInitialState
History.handleInitialState = function(base){
if (!base) base = '';
var location = window.location,
pathname = location.pathname.substr(base.length),
pathname = History.getUrlPart(location.href).substr(base.length),
hash = location.hash,
hasPushState = History.hasPushState();

Expand All @@ -31,7 +31,7 @@ History.handleInitialState = function(base){
if (!hash || hash.length <= 1) return false;
if (hasPushState){
(function(){
History.push(hash.substr(1));
History.push(History.getUrlPart(hash.substr(1)));
}).delay(1);
return false;
}
Expand Down
22 changes: 16 additions & 6 deletions Source/History.js
Expand Up @@ -20,7 +20,13 @@ provides: History

var events = Element.NativeEvents,
location = window.location,
base = location.pathname,
getUrlPart = function(url){
if(url.substr(0,8) == 'https://' || url.substr(0,7) == 'http://'){
url = '/'+url.split('/').slice(3).join('/');
}
return url.split('#')[0];
},
base = getUrlPart(location.href),
history = window.history,
hasPushState = ('pushState' in history),
event = hasPushState ? 'popstate' : 'hashchange';
Expand All @@ -42,24 +48,28 @@ this.History = new new Class({
this.timer = this.check.periodical(200, this);
},

getUrlPart: getUrlPart,

push: hasPushState ? function(url, title, state){
url = getUrlPart(url);
if (base && base != url) base = null;

history.pushState(state || null, title || null, url);
this.onChange(url, state);
} : function(url){
location.hash = url;
location.hash = getUrlPart(url);
},

replace: hasPushState ? function(url, title, state){
history.replaceState(state || null, title || null, url);
history.replaceState(state || null, title || null, getUrlPart(url));
} : function(url){
url = getUrlPart(url);
this.hash = '#' + url;
this.push(url);
},

pop: hasPushState ? function(event){
var url = location.pathname;
var url = getUrlPart(location.href);
if (url == base){
base = null;
return;
Expand All @@ -70,7 +80,7 @@ this.History = new new Class({
if (this.hash == hash) return;

this.hash = hash;
this.onChange(hash.substr(1));
this.onChange(getUrlPart(hash.substr(1)));
},

onChange: function(url, state){
Expand All @@ -86,7 +96,7 @@ this.History = new new Class({
},

getPath: function(){
return hasPushState ? location.pathname : location.hash.substr(1);
return hasPushState ? getUrlPart(location.href) : getUrlPart(location.hash.substr(1));
},

hasPushState: function(){
Expand Down

0 comments on commit 7f3e62d

Please sign in to comment.