Skip to content

Instantly share code, notes, and snippets.

@think49
Last active December 1, 2015 03:56
Show Gist options
  • Save think49/1e8944a19a36dd57c829 to your computer and use it in GitHub Desktop.
Save think49/1e8944a19a36dd57c829 to your computer and use it in GitHub Desktop.
to-absolute-url.js: URL文字列を絶対URLに変換 (※DOM L2 HTML の HTMLAnchorElement#href 依存)
/**
* 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