{"id":7272,"date":"2025-01-27T17:19:53","date_gmt":"2025-01-27T08:19:53","guid":{"rendered":"https:\/\/code-plus.jp\/gp\/?p=7272"},"modified":"2026-01-08T11:13:10","modified_gmt":"2026-01-08T02:13:10","slug":"post-7272","status":"publish","type":"post","link":"https:\/\/code-plus.jp\/gp\/post-7272\/","title":{"rendered":"JavaScript\u3067Web\u30da\u30fc\u30b8\u306eBasic\u8a8d\u8a3c\u72b6\u614b\u3092\u8868\u793a\u3059\u308b"},"content":{"rendered":"<p>\u958b\u767a\u74b0\u5883\u3084\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u3067Basic\u8a8d\u8a3c\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u306f\u4e00\u822c\u7684\u306a practice \u3067\u3059\u304c\u3001\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u30c6\u30b9\u30c8\u306a\u3069\u3067\u4e00\u6642\u7684\u306b\u89e3\u9664\u3057\u305f\u5f8c\u3001\u305d\u306e\u307e\u307e\u89e3\u9664\u72b6\u614b\u306b\u306a\u3063\u3066\u3044\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u4e8b\u614b\u3092\u9632\u3050\u305f\u3081\u3001Basic\u8a8d\u8a3c\u306e\u72b6\u614b\u3092\u8996\u899a\u7684\u306b\u78ba\u8a8d\u3067\u304d\u308b\u4ed5\u7d44\u307f\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\r\n<h2>\u5b9f\u88c5\u306e\u30dd\u30a4\u30f3\u30c8<\/h2>\r\n<ol style=\"list-style-type:decimal;\">\r\n<li>Basic\u8a8d\u8a3c\u306e\u72b6\u614b\u78ba\u8a8d<\/li>\r\n<li>\u8996\u899a\u7684\u306a\u30d5\u30a3\u30fc\u30c9\u30d0\u30c3\u30af<\/li>\r\n<li>\u8a2d\u7f6e\u30fb\u524a\u9664\u306e\u5bb9\u6613\u3055<\/li>\r\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3078\u306e\u914d\u616e<\/li>\r\n<\/ol>\r\n<h2>\u30b3\u30fc\u30c9\u5b9f\u88c5<\/h2>\r\n<pre><code class=\"language-javascript\">(function () {\r\n  \/\/ \u30b9\u30bf\u30a4\u30eb\u306e\u5b9a\u7fa9\u3068\u8ffd\u52a0\r\n  const STYLE = document.createElement(&#39;style&#39;);\r\n  STYLE.textContent = `\r\n    .auth-status {\r\n      position: fixed;\r\n      z-index: 100000;\r\n      top: 0px;\r\n      right: 0px;\r\n      border-radius: 0px 0px 0px 8px;\r\n      padding: 4px 1.5pc;\r\n      line-height: 1;\r\n      font-size: 14px;\r\n      white-space: nowrap;\r\n      background: #49B8FD;\r\n      color: #FFF;\r\n      display: none;\r\n    }\r\n  `;\r\n  document.getElementsByTagName(&#39;head&#39;)[0].appendChild(STYLE);\r\n\r\n  \/\/ Basic\u8a8d\u8a3c\u306e\u78ba\u8a8d\u95a2\u6570\r\n  function checkBasicAuth() {\r\n    document.getElementsByTagName(&#39;body&#39;)[0].insertAdjacentHTML(\r\n      &#39;afterbegin&#39;,\r\n      &#39;&lt;div id=&quot;authStatus&quot; class=&quot;auth-status&quot;&gt;Basic\u8a8d\u8a3c\u8a2d\u5b9a\u4e2d&lt;\/div&gt;&#39;\r\n    );\r\n\r\n    fetch(window.location.href, {\r\n      method: &#39;HEAD&#39;\r\n    })\r\n      .then(response =&gt; {\r\n        const authHeader = response.headers.get(&#39;WWW-Authenticate&#39;);\r\n        if (authHeader &amp;&amp; authHeader.toLowerCase().startsWith(&#39;basic&#39;)) {\r\n          document.getElementById(&#39;authStatus&#39;).style.display = &#39;block&#39;;\r\n        }\r\n      })\r\n      .catch(error =&gt; console.error(&#39;\u8a8d\u8a3c\u78ba\u8a8d\u30a8\u30e9\u30fc:&#39;, error));\r\n  }\r\n\r\n  \/\/ \u30da\u30fc\u30b8\u8aad\u307f\u8fbc\u307f\u6642\u306b\u5b9f\u884c\r\n  window.addEventListener(&#39;load&#39;, checkBasicAuth);\r\n})();\r\n<\/code><\/pre>\r\n<h2>\u5b9f\u88c5\u306e\u89e3\u8aac<\/h2>\r\n<h3>1. \u30b3\u30fc\u30c9\u306e\u69cb\u9020<\/h3>\r\n<ul style=\"list-style-type:bullet;\">\r\n<li>\u5373\u6642\u5b9f\u884c\u95a2\u6570\u3067\u30e9\u30c3\u30d7\u3059\u308b\u3053\u3068\u3067\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3092\u6c5a\u67d3\u3057\u306a\u3044<\/li>\r\n<li>\u30b9\u30bf\u30a4\u30eb\u3092\u52d5\u7684\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u5225\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u3092\u4e0d\u8981\u306b\u3059\u308b<\/li>\r\n<li><code>insertAdjacentHTML<\/code> \u3092\u4f7f\u7528\u3057\u3066\u52b9\u7387\u7684\u306bDOM\u8981\u7d20\u3092\u8ffd\u52a0<\/li>\r\n<\/ul>\r\n<h3>2. \u30b9\u30bf\u30a4\u30ea\u30f3\u30b0\u306e\u5de5\u592b<\/h3>\r\n<ul style=\"list-style-type:bullet;\">\r\n<li><code>position: fixed<\/code> \u3067\u753b\u9762\u53f3\u4e0a\u306b\u56fa\u5b9a\u8868\u793a<\/li>\r\n<li><code>z-index: 100000<\/code> \u3067\u6700\u524d\u9762\u8868\u793a\u3092\u4fdd\u8a3c<\/li>\r\n<li>\u5de6\u4e0b\u306e\u307f\u89d2\u4e38\u306b\u3059\u308b\u3053\u3068\u3067\u8996\u8a8d\u6027\u3092\u5411\u4e0a<\/li>\r\n<li><code>white-space: nowrap<\/code> \u3067\u30c6\u30ad\u30b9\u30c8\u306e\u6298\u308a\u8fd4\u3057\u3092\u9632\u6b62<\/li>\r\n<li>\u660e\u308b\u3044\u9752\u8272\u306e\u80cc\u666f\u3067\u8b66\u544a\u7684\u304b\u3064\u76ee\u306b\u512a\u3057\u3044\u914d\u8272<\/li>\r\n<\/ul>\r\n<h3>3. Basic\u8a8d\u8a3c\u306e\u691c\u51fa<\/h3>\r\n<ul style=\"list-style-type:bullet;\">\r\n<li><code>fetch<\/code> API\u3092\u4f7f\u7528\u3057\u3066\u30d8\u30c3\u30c0\u30fc\u60c5\u5831\u3092\u53d6\u5f97<\/li>\r\n<li><code>WWW-Authenticate<\/code> \u30d8\u30c3\u30c0\u30fc\u306e\u5b58\u5728\u3068\u305d\u306e\u5024\u3092\u78ba\u8a8d<\/li>\r\n<li>\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3082\u5b9f\u88c5<\/li>\r\n<\/ul>\r\n<h2>\u4f7f\u7528\u65b9\u6cd5<\/h2>\r\n<ol style=\"list-style-type:decimal;\">\r\n<li>\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3092 <code>auth_status.js<\/code> \u3068\u3057\u3066\u4fdd\u5b58<\/li>\r\n<li>HTML\u30d5\u30a1\u30a4\u30eb\u306e <code>head<\/code> \u30bf\u30b0\u5185\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8aad\u307f\u8fbc\u3080<\/li>\r\n<\/ol>\r\n<pre><code class=\"language-html\">&lt;head&gt;\r\n  &lt;!-- Basic\u8a8d\u8a3c\u306e\u72b6\u614b\u3092\u8868\u793a\u3059\u308b --&gt;\r\n  &lt;script src=&quot;\/auth_status.js&quot;&gt;&lt;\/script&gt;\r\n  &lt;!-- \u4ed6\u306ehead\u8981\u7d20 --&gt;\r\n&lt;\/head&gt;\r\n<\/code><\/pre>\r\n<h2>\u904b\u7528\u4e0a\u306e\u6ce8\u610f\u70b9<\/h2>\r\n<ul style=\"list-style-type:bullet;\">\r\n<li>\u30d7\u30ed\u30c0\u30af\u30b7\u30e7\u30f3\u74b0\u5883\u306b\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u524d\u306b\u3001\u3053\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u3092\u5fd8\u308c\u305a\u306b<\/li>\r\n<li>\u30c7\u30d7\u30ed\u30a4\u30e1\u30f3\u30c8\u30c1\u30a7\u30c3\u30af\u30ea\u30b9\u30c8\u306b\u300cBasic\u8a8d\u8a3c\u72b6\u614b\u78ba\u8a8d\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u524a\u9664\u78ba\u8a8d\u300d\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3092\u63a8\u5968<\/li>\r\n<li>\u958b\u767a\u30c1\u30fc\u30e0\u3067\u8a8d\u8a3c\u72b6\u614b\u306e\u8996\u899a\u7684\u306a\u78ba\u8a8d\u3092\u7fd2\u6163\u5316<\/li>\r\n<\/ul>\r\n<h2>\u30e1\u30ea\u30c3\u30c8<\/h2>\r\n<ol style=\"list-style-type:decimal;\">\r\n<li><strong>\u8996\u899a\u7684\u306a\u78ba\u8a8d<\/strong>: Basic\u8a8d\u8a3c\u306e\u72b6\u614b\u304c\u4e00\u76ee\u3067\u5206\u304b\u308b<\/li>\r\n<li><strong>\u7c21\u5358\u306a\u5b9f\u88c5<\/strong>: \u4e00\u3064\u306eJavaScript\u30d5\u30a1\u30a4\u30eb\u3067\u5b8c\u7d50<\/li>\r\n<li><strong>\u8efd\u91cf<\/strong>: \u5fc5\u8981\u6700\u5c0f\u9650\u306e\u30b3\u30fc\u30c9\u3067\u5b9f\u88c5<\/li>\r\n<li><strong>\u4fdd\u5b88\u6027<\/strong>: \u30b3\u30fc\u30c9\u306e\u8ffd\u52a0\u30fb\u524a\u9664\u304c\u5bb9\u6613<\/li>\r\n<\/ol>\r\n<h2>\u307e\u3068\u3081<\/h2>\r\n<p>Basic\u8a8d\u8a3c\u306e\u72b6\u614b\u3092\u8996\u899a\u7684\u306b\u78ba\u8a8d\u3067\u304d\u308b\u4ed5\u7d44\u307f\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067\u3001\u8a8d\u8a3c\u8a2d\u5b9a\u306e\u7ba1\u7406\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002\u7279\u306b\u8907\u6570\u306e\u74b0\u5883\u3092\u6271\u3046\u958b\u767a\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306f\u3001\u3053\u306e\u3088\u3046\u306a\u8996\u899a\u7684\u306a\u30d5\u30a3\u30fc\u30c9\u30d0\u30c3\u30af\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\r\n<p>\u672c\u5b9f\u88c5\u306f\u5fc5\u8981\u6700\u5c0f\u9650\u306e\u30b3\u30fc\u30c9\u3067\u52b9\u679c\u7684\u306a\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u3001\u958b\u767a\u74b0\u5883\u3067\u306e\u904b\u7528\u3092\u3088\u308a\u78ba\u5b9f\u306b\u3059\u308b\u30c4\u30fc\u30eb\u3068\u3057\u3066\u6d3b\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\r\n","protected":false},"excerpt":{"rendered":"\u958b\u767a\u74b0\u5883\u3084\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u3067Basic\u8a8d\u8a3c\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u306f\u4e00\u822c\u7684\u306a practice \u3067\u3059\u304c\u3001\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u30c6\u30b9\u30c8\u306a\u3069\u3067\u4e00\u6642\u7684\u306b\u89e3\u9664\u3057\u305f\u5f8c\u3001\u305d\u306e\u307e\u307e\u89e3\u9664\u72b6\u614b\u306b\u306a\u3063\u3066\u3044\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306a\u4e8b\u614b\u3092\u9632\u3050\u305f\u3081\u3001[...]","protected":false},"author":1,"featured_media":7274,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","footnotes":"","vk-ltc-link":"","vk-ltc-target":"0"},"categories":[12],"tags":[85,84,86],"group":[],"class_list":["post-7272","post","type-post","status-publish","format-standard","has-post-thumbnail","category-javascript","tag-85","tag-84","tag-86"],"acf":[],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/7272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/comments?post=7272"}],"version-history":[{"count":0,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/7272\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media\/7274"}],"wp:attachment":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media?parent=7272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/categories?post=7272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/tags?post=7272"},{"taxonomy":"group","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/group?post=7272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}