Skip to content

Instantly share code, notes, and snippets.

@sounisi5011
Last active March 13, 2018 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sounisi5011/8b5949315c3bb3226895d1ebcb7b249c to your computer and use it in GitHub Desktop.
Save sounisi5011/8b5949315c3bb3226895d1ebcb7b249c to your computer and use it in GitHub Desktop.
URLインターフェイスに対応しているかをテストする
<!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>
(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