hashchange: Extract get_full_url().

This commit is contained in:
Steve Howell 2018-12-06 21:49:26 +00:00 committed by Tim Abbott
parent ef77496897
commit 0914037bed
1 changed files with 21 additions and 16 deletions

View File

@ -6,27 +6,32 @@ var exports = {};
var changing_hash = false;
function set_hash(hash) {
function get_full_url(hash) {
var location = window.location;
if (hash === '' || hash.charAt(0) !== '#') {
hash = '#' + hash;
}
// IE returns pathname as undefined and missing the leading /
var pathname = location.pathname;
if (pathname === undefined) {
pathname = '/';
} else if (pathname === '' || pathname.charAt(0) !== '/') {
pathname = '/' + pathname;
}
// Build a full URL to not have same origin problems
var url = location.protocol + '//' + location.host + pathname + hash;
return url;
}
function set_hash(hash) {
if (history.pushState) {
if (hash === '' || hash.charAt(0) !== '#') {
hash = '#' + hash;
}
// IE returns pathname as undefined and missing the leading /
var pathname = location.pathname;
if (pathname === undefined) {
pathname = '/';
} else if (pathname === '' || pathname.charAt(0) !== '/') {
pathname = '/' + pathname;
}
// Build a full URL to not have same origin problems
var url = location.protocol + '//' + location.host + pathname + hash;
var url = get_full_url(hash);
history.pushState(null, null, url);
} else {
location.hash = hash;
window.location.hash = hash;
}
}