Last active
March 13, 2018 08:40
-
-
Save sounisi5011/8b5949315c3bb3226895d1ebcb7b249c to your computer and use it in GitHub Desktop.
URLインターフェイスに対応しているかをテストする
This file contains 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
<!DOCTYPE html> | |
<meta charset=utf-8> | |
<meta name=viewport content="width=device-width,initial-scale=1"> | |
<meta name=format-detection content="telephone=no,email=no,address=no"> | |
<title>URLインターフェイスに対応しているかをテストする</title> | |
<h1>URLインターフェイスに対応しているかをテストする</h1> | |
<pre id=main>JavaScriptに対応していません</pre> | |
<h2>Gist</h2> | |
<p><a href="https://gist.github.com/sounisi5011/8b5949315c3bb3226895d1ebcb7b249c">https://gist.github.com/sounisi5011/8b5949315c3bb3226895d1ebcb7b249c</a> | |
<script src="main.js"></script> |
This file contains 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
(function(global, mainElem) { | |
'use strict'; | |
function notSupportedMsg(msg) { | |
mainElem.textContent = 'URLインターフェイスに対応していません:' + msg; | |
} | |
if (!mainElem) { | |
return; | |
} | |
var URL = global.URL; | |
if (typeof URL !== 'function') { | |
notSupportedMsg('URLコンストラクタ関数が未実装です'); | |
return; | |
} | |
var url; | |
try { | |
url = new URL('http://example.com'); | |
} catch(_) { | |
notSupportedMsg('new URL()によるURLオブジェクトの生成に失敗しました'); | |
return; | |
} | |
/** | |
* @see https://url.spec.whatwg.org/commit-snapshots/5fc4c4c17f3077beda5e3647df73f4b1b0ba7084/#url-class | |
*/ | |
var notSupportedPropList = ['href', 'origin', 'protocol', 'username', 'password', 'host', 'hostname', 'port', 'pathname', 'search', 'searchParams', 'hash'] | |
.filter(function(prop) { | |
return !(prop in url); | |
}); | |
if (0 < notSupportedPropList.length) { | |
notSupportedMsg( | |
'URLオブジェクトは' + | |
notSupportedPropList | |
.map(function(prop) { | |
return prop + 'プロパティ'; | |
}) | |
.join('、') + | |
'を持っていません' | |
); | |
return; | |
} | |
mainElem.textContent = 'URLインターフェイスに対応しています'; | |
})(Function('return this')(), document.getElementById('main')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment