Last active
December 1, 2015 03:56
-
-
Save think49/1e8944a19a36dd57c829 to your computer and use it in GitHub Desktop.
to-absolute-url.js: URL文字列を絶対URLに変換 (※DOM L2 HTML の HTMLAnchorElement#href 依存)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* to-absolute-url.js | |
* | |
* @version 1.0.1 | |
* @author think49 | |
* @url https://gist.github.com/think49/1e8944a19a36dd57c829 | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
*/ | |
'use strict'; | |
var toAbsoluteUrl = (function (currentDirectory, anchorElement) { | |
var toAbsoluteUrl; | |
function toAbsoluteUrlByHref (url) { | |
anchorElement.href = url; | |
return anchorElement.href; | |
} | |
function toAbsoluteUrlByCloneHref (url) { | |
anchorElement.href = url; | |
return anchorElement.cloneNode(false).href; // for IE6, IE7 | |
} | |
if (toAbsoluteUrlByHref('t') === currentDirectory + 't') { | |
toAbsoluteUrl = toAbsoluteUrlByHref; | |
} else if (toAbsoluteUrlByCloneHref('t') === currentDirectory + 't') { | |
toAbsoluteUrl = toAbsoluteUrlByCloneHref; | |
} else { | |
throw new Error; | |
} | |
return toAbsoluteUrl; | |
})(location.protocol + '//' + location.host + location.pathname.replace(/[^\x2f]+$/, ''), document.createElement('a')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment