Skip to content

Commit fc628e6

Browse files
committed
URLs in the State Hashes for HTML4 Browsers are now even shorter
1 parent 553c355 commit fc628e6

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Thanks! every bit of help really does make a difference. Again thank you.
238238
- Fixed Session Storage Issue - Reported by a whole bunch of different people; [one](https://github.com/balupton/History.js/issues#issue/36), [two](https://github.com/balupton/History.js/issues#issue/37), [three](http://getsatisfaction.com/balupton/topics/history_js_1_6_losing_state_after_manual_page_reload)
239239
- Fixed URL Encoding Issue - [Reported](https://github.com/balupton/history.js/issues/#issue/33) by [Rob Madole](http://robmadole.com/)
240240
- Disabled support for IE6,7,8 when using the Prototype Adapter (there is nothing we can do about this, it is due to a bug in the prototype library) - [Reported](https://github.com/balupton/history.js/issues#issue/39) by [Sindre Wimberger](http://sindre.at/)
241+
- URLs in the State Hashes for HTML4 Browsers are now even shorter - [Discussion](https://github.com/balupton/history.js/issues#issue/28)
241242

242243
- v1.6.0 - March 22 2011
243244
- Added Zepto adapter thanks to [Matt Garrett](http://twitter.com/#!/matthewgarrett)

scripts/uncompressed/history.html4.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,17 @@
414414
History.saveHash(currentHash);
415415

416416
// Expand Hash
417-
currentState = History.extractState(currentHash||document.location.href,true);
418-
if ( !currentState ) {
417+
if ( currentHash && History.isTraditionalAnchor(currentHash) ) {
419418
//History.debug('History.onHashChange: traditional anchor', currentHash);
420419
// Traditional Anchor Hash
421420
History.Adapter.trigger(window,'anchorchange');
422421
History.busy(false);
423422
return false;
424423
}
425424

425+
// Create State
426+
currentState = History.extractState(History.getFullUrl(currentHash||document.location.href,false),true);
427+
426428
// Check if we are the same state
427429
if ( History.isLastSavedState(currentState) ) {
428430
//History.debug('History.onHashChange: no change');

scripts/uncompressed/history.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,13 @@
408408
* History.getFullUrl(url)
409409
* Ensures that we have an absolute URL and not a relative URL
410410
* @param {string} url
411+
* @param {Boolean} allowBaseHref
411412
* @return {string} fullUrl
412413
*/
413-
History.getFullUrl = function(url){
414+
History.getFullUrl = function(url,allowBaseHref){
414415
// Prepare
415416
var fullUrl = url, firstChar = url.substring(0,1);
417+
allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
416418

417419
// Check
418420
if ( /[a-z]+\:\/\//.test(url) ) {
@@ -432,7 +434,16 @@
432434
}
433435
else {
434436
// Relative URL
435-
fullUrl = History.getBaseUrl()+url;
437+
if ( allowBaseHref ) {
438+
fullUrl = History.getBaseUrl()+url.replace(/^(\.\/)+/,'');
439+
} else {
440+
fullUrl = History.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
441+
}
442+
// We have an if condition above as we do not want hashes
443+
// which are relative to the baseHref in our URLs
444+
// as if the baseHref changes, then all our bookmarks
445+
// would now point to different locations
446+
// whereas the basePageUrl will always stay the same
436447
}
437448

438449
// Return
@@ -447,13 +458,29 @@
447458
*/
448459
History.getShortUrl = function(url){
449460
// Prepare
450-
var shortUrl, rootUrl = History.getRootUrl(); // History.getBaseHref()||History.getBasePageUrl()
461+
var shortUrl = url, baseUrl = History.getBaseUrl(), rootUrl = History.getRootUrl();
462+
463+
// Trim baseUrl
464+
if ( History.emulated.pushState ) {
465+
// We are in a if statement as when pushState is not emulated
466+
// The actual url these short urls are relative to can change
467+
// So within the same session, we the url may end up somewhere different
468+
shortUrl = shortUrl.replace(baseUrl,'');
469+
}
451470

452-
// Adjust
453-
shortUrl = url.replace(rootUrl,'/');
471+
// Trim rootUrl
472+
shortUrl = shortUrl.replace(rootUrl,'/');
473+
474+
// Ensure we can still detect it as a state
475+
if ( History.isTraditionalAnchor(shortUrl) ) {
476+
shortUrl = './'+shortUrl;
477+
}
478+
479+
// Clean It
480+
shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
454481

455482
// Return
456-
return shortUrl.replace(/\#$/,'');
483+
return shortUrl;
457484
};
458485

459486
// ----------------------------------------------------------------------
@@ -742,9 +769,25 @@
742769
return id||false;
743770
};
744771

772+
/**
773+
* History.isTraditionalAnchor
774+
* Checks to see if the url is a traditional anchor or not
775+
* @param {String} url_or_hash
776+
* @return {Boolean}
777+
*/
778+
History.isTraditionalAnchor = function(url_or_hash){
779+
// Check
780+
var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
781+
782+
// Return
783+
return isTraditional;
784+
};
785+
745786
/**
746787
* History.extractState
747788
* Get a State by it's URL or Hash
789+
* @param {String} url_or_hash
790+
* @return {State|null}
748791
*/
749792
History.extractState = function(url_or_hash,create){
750793
// Prepare
@@ -769,7 +812,7 @@
769812
}
770813

771814
// Create State
772-
if ( !State && create && /\//.test(url_or_hash) ) {
815+
if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) {
773816
State = History.createStateObject(null,null,url);
774817
}
775818
}
@@ -1088,22 +1131,6 @@
10881131
return hash;
10891132
};
10901133

1091-
/**
1092-
* History.isTraditionalAnchor(url)
1093-
* Checks to see if the url is a traditional anchor
1094-
* @param {string} url
1095-
* @return {boolean}
1096-
*/
1097-
History.isTraditionalAnchor = function(url){
1098-
var
1099-
hash = History.getHashByUrl(url),
1100-
el = document.getElementById(hash),
1101-
isTraditionalAnchor = typeof el !== 'undefined';
1102-
1103-
// Return isTraditionalAnchor
1104-
return isTraditionalAnchor;
1105-
};
1106-
11071134
/**
11081135
* History.setTitle(title)
11091136
* Applies the title to the document

0 commit comments

Comments
 (0)