JavaScriptã§baseãæå®ãã¦ç¸å¯¾URLã絶対URLã«å¤æãã
ã¡ã¢ãããã
baseã¨ãªãURLãæå®ãã¦ç¸å¯¾URLã絶対URLã«å¤æããã«ã¯ãChromeãFireofxã§ã¯URLUtilsãç¨ãã¦ä»¥ä¸ã®ããã«æ¸ããã¨ã§ç°¡åã«å®ç¾ã§ããã
var absolute = (new URL( "foo", "http://example.jp/bar/baz" ) ).href; // http://example.jp/bar/foo
IEã§ã¯URLã³ã³ã¹ãã©ã¯ã¿ã¯ãµãã¼ãããã¦ããªãããIE9以ä¸ã§ã¯DOMParserãcreateHTMLDocumentã使ã£ã¦ç¾å¨ã®DOMã¨ã¯åé¢ããdocumentãçæãããã®ä¸ã«<base>è¦ç´ ãç¨ãã¦base URLãæå®ãããã®documentå ã§<a>è¦ç´ ãç¨ãã¦ç¸å¯¾URLã絶対URLã«å¤æããã¨ããæ段ã«ãã£ã¦ç¸å¯¾URLã絶対URLã«å¤æå¯è½ã§ããã
function getAbsoluteUrl( url, base ){ var doc, baseElm; var fallback = function( doc, url ){ var elm = doc.createElement( "a" ); elm.setAttribute( "href", "/test" ); if( elm.href === "/test" ){ // IE6, IE7 elm.setAttribute( "href", url ); return elm.getAttribute( "href", 4 ); }else{ elm.setAttribute( "href", url ); return elm.href; } } if( base === undefined ){ try{ return (new URL( url, loation.href )).href; }catch( e ){ return fallback( document, url ); } }else{ try{ return (new URL( url, base )).href; }catch( e ){ if( typeof DOMParser !== "undefined" ){ try{ var parser = new DOMParser(); doc = parser.parseFromString( "<html><head></head><body></body>", "text/html" ); baseElm = doc.createElement( "base" ); baseElm.setAttribute( "href", base ); doc.head.appendChild( baseElm ); }catch( e ){ doc = undefined; } } if( !doc && document.implementation && document.implementation.createHTMLDocument ){ try{ doc = document.implementation.createHTMLDocument(""); baseElm = doc.createElement( "base" ); baseElm.setAttribute( "href", base ); doc.head.appendChild( baseElm ); }catch( e ){ doc = undefined; } } if( doc ){ return fallback( doc, url ); }else{ return ""; } } } }