|
408 | 408 | * History.getFullUrl(url)
|
409 | 409 | * Ensures that we have an absolute URL and not a relative URL
|
410 | 410 | * @param {string} url
|
| 411 | + * @param {Boolean} allowBaseHref |
411 | 412 | * @return {string} fullUrl
|
412 | 413 | */
|
413 |
| - History.getFullUrl = function(url){ |
| 414 | + History.getFullUrl = function(url,allowBaseHref){ |
414 | 415 | // Prepare
|
415 | 416 | var fullUrl = url, firstChar = url.substring(0,1);
|
| 417 | + allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref; |
416 | 418 |
|
417 | 419 | // Check
|
418 | 420 | if ( /[a-z]+\:\/\//.test(url) ) {
|
|
432 | 434 | }
|
433 | 435 | else {
|
434 | 436 | // 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 |
436 | 447 | }
|
437 | 448 |
|
438 | 449 | // Return
|
|
447 | 458 | */
|
448 | 459 | History.getShortUrl = function(url){
|
449 | 460 | // 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 | + } |
451 | 470 |
|
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(/\#$/,''); |
454 | 481 |
|
455 | 482 | // Return
|
456 |
| - return shortUrl.replace(/\#$/,''); |
| 483 | + return shortUrl; |
457 | 484 | };
|
458 | 485 |
|
459 | 486 | // ----------------------------------------------------------------------
|
|
742 | 769 | return id||false;
|
743 | 770 | };
|
744 | 771 |
|
| 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 | + |
745 | 786 | /**
|
746 | 787 | * History.extractState
|
747 | 788 | * Get a State by it's URL or Hash
|
| 789 | + * @param {String} url_or_hash |
| 790 | + * @return {State|null} |
748 | 791 | */
|
749 | 792 | History.extractState = function(url_or_hash,create){
|
750 | 793 | // Prepare
|
|
769 | 812 | }
|
770 | 813 |
|
771 | 814 | // Create State
|
772 |
| - if ( !State && create && /\//.test(url_or_hash) ) { |
| 815 | + if ( !State && create && !History.isTraditionalAnchor(url_or_hash) ) { |
773 | 816 | State = History.createStateObject(null,null,url);
|
774 | 817 | }
|
775 | 818 | }
|
|
1088 | 1131 | return hash;
|
1089 | 1132 | };
|
1090 | 1133 |
|
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 |
| - |
1107 | 1134 | /**
|
1108 | 1135 | * History.setTitle(title)
|
1109 | 1136 | * Applies the title to the document
|
|
0 commit comments