{"id":5720,"date":"2024-08-03T21:21:34","date_gmt":"2024-08-03T12:21:34","guid":{"rendered":"http:\/\/www.code-plus.jp\/240909\/?p=5720"},"modified":"2026-01-08T11:13:02","modified_gmt":"2026-01-08T02:13:02","slug":"post-5720","status":"publish","type":"post","link":"https:\/\/code-plus.jp\/gp\/post-5720\/","title":{"rendered":"\u753b\u50cf\u30d1\u30b9\u3092\u52d5\u7684\u306b\u5909\u66f4\u3059\u308bJavaScript\u30c6\u30af\u30cb\u30c3\u30af"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u306f\u3058\u3081\u306b<\/h2>\n\n\n<p class=\"wp-block-paragraph\">Web\u30b5\u30a4\u30c8\u958b\u767a\u306b\u304a\u3044\u3066\u3001\u753b\u50cf\u30d1\u30b9\u3092\u52d5\u7684\u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u751f\u3058\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u306f\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u3068\u672c\u756a\u74b0\u5883\u3067\u753b\u50cf\u30d1\u30b9\u304c\u7570\u306a\u308b\u5834\u5408\u306b\u3001JavaScript\u3092\u4f7f\u7528\u3057\u3066\u52d5\u7684\u306b\u753b\u50cf\u30d1\u30b9\u3092\u5909\u66f4\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">\u306a\u305c\u753b\u50cf\u30d1\u30b9\u306e\u52d5\u7684\u5909\u66f4\u304c\u5fc5\u8981\u306a\u306e\u304b\uff1f<\/h2>\n\n\n<p class=\"wp-block-paragraph\">\u753b\u50cf\u30d1\u30b9\u306e\u52d5\u7684\u5909\u66f4\u304c\u5fc5\u8981\u306b\u306a\u308b\u30b1\u30fc\u30b9\u306f\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\uff1a<\/p>\n\n\n<ol class=\"wp-block-list\">\n\t<li>\u958b\u767a\u74b0\u5883\u3068\u672c\u756a\u74b0\u5883\u306e\u9055\u3044<\/li>\n\n\n\t<li>CMS\u79fb\u884c<\/li>\n\n\n\t<li>\u30c9\u30e1\u30a4\u30f3\u5909\u66f4<\/li>\n\n\n\t<li>CDN\u5c0e\u5165<\/li>\n\n\n\t<li>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u5f37\u5316\uff08HTTPS\u3078\u306e\u79fb\u884c\uff09<\/li>\n\n\n\t<li>\u30b5\u30fc\u30d0\u30fc\u69cb\u6210\u306e\u5909\u66f4<\/li>\n\n\n\t<li>\u56fd\u969b\u5316\u5bfe\u5fdc<\/li>\n\n\n\t<li>A\/B\u30c6\u30b9\u30c8<\/li>\n\n\n\t<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316<\/li>\n\n\n\t<li>\u30b3\u30f3\u30c6\u30f3\u30c4\u30c7\u30ea\u30d0\u30ea\u30fc\u306e\u6700\u9069\u5316<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">\u4eca\u56de\u306f\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u3068\u672c\u756a\u74b0\u5883\u3067\u753b\u50cf\u30d1\u30b9\u304c\u7570\u306a\u308b\u5834\u5408\u3092\u4f8b\u306b\u3001\u89e3\u6c7a\u65b9\u6cd5\u3092\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">\u5b9f\u88c5\u4f8b<\/h2>\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u306f\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u3067\u306e\u307f\u753b\u50cf\u30d1\u30b9\u3092\u5909\u66f4\u3059\u308bJavaScript\u306e\u30b3\u30fc\u30c9\u3067\u3059\uff1a<\/p>\n\n\n<pre class=\"language-javascript\"><code>document.addEventListener('DOMContentLoaded', function() {\n\/\/ \u30c6\u30b9\u30c8\u74b0\u5883\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\nif (window.location.href.includes('example-test.com')) {\n  const oldPath = '\/content\/images\/main\/2024\/';\n  const newPath = '\/test-images\/2024\/';\n\n  \/\/ img \u3068 source \u30bf\u30b0\u306e\u5c5e\u6027\u3092\u5909\u66f4\n  const mediaElements = document.querySelectorAll('img, source');\n  mediaElements.forEach(el =&gt; {\n    const attributesToChange = ['src', 'srcset', 'data-src', 'data-src0', 'data-src744'];\n    attributesToChange.forEach(attr =&gt; {\n      if (el.hasAttribute(attr) &amp;&amp; el.getAttribute(attr).includes(oldPath)) {\n        el.setAttribute(attr, el.getAttribute(attr).replace(new RegExp(oldPath, 'g'), newPath));\n      }\n    });\n  });\n\n  \/\/ style\u5c5e\u6027\u5185\u306ebackground-image\u3092\u5909\u66f4\n  const elementsWithBgImage = document.querySelectorAll('[style*=\"background-image\"]');\n  elementsWithBgImage.forEach(el =&gt; {\n    const style = el.getAttribute('style');\n    if (style.includes(oldPath)) {\n      const newStyle = style.replace(new RegExp(oldPath, 'g'), newPath);\n      el.setAttribute('style', newStyle);\n    }\n  });\n\n  \/\/ \u30a4\u30f3\u30e9\u30a4\u30f3\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u5185\u306eurl()\u3092\u5909\u66f4\n  const styleSheets = document.styleSheets;\n  for (let i = 0; i &lt; styleSheets.length; i++) {\n    const rules = styleSheets[i].cssRules || styleSheets[i].rules;\n    for (let j = 0; j &lt; rules.length; j++) {\n      if (rules[j].style &amp;&amp; rules[j].style.backgroundImage) {\n        rules[j].style.backgroundImage = rules[j].style.backgroundImage.replace(new RegExp(oldPath, 'g'), newPath);\n      }\n    }\n  }\n}\n});<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\">\u30b3\u30fc\u30c9\u306e\u89e3\u8aac<\/h2>\n\n\n<ol class=\"wp-block-list\">\n\t<li><code>DOMContentLoaded<\/code> \u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc\u3092\u4f7f\u7528\u3057\u3066\u3001DOM\u306e\u8aad\u307f\u8fbc\u307f\u304c\u5b8c\u4e86\u3057\u305f\u5f8c\u306b\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/li>\n\n\n\t<li><code>window.location.href.includes('example-test.com')<\/code> \u3067\u3001\u73fe\u5728\u306eURL\u306b\u30c6\u30b9\u30c8\u74b0\u5883\u306e\u30c9\u30e1\u30a4\u30f3\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n\n\n\t<li><code>img<\/code> \u3068 <code>source<\/code> \u30bf\u30b0\u306e <code>src<\/code>, <code>srcset<\/code>, <code>data-src<\/code>, <code>data-src0<\/code>, <code>data-src744<\/code> \u5c5e\u6027\u3092\u5909\u66f4\u3057\u307e\u3059\u3002<\/li>\n\n\n\t<li><code>style<\/code> \u5c5e\u6027\u5185\u306e <code>background-image<\/code> \u3092\u5909\u66f4\u3057\u307e\u3059\u3002<\/li>\n\n\n\t<li>\u30a4\u30f3\u30e9\u30a4\u30f3\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u5185\u306e <code>url()<\/code> \u95a2\u6570\u3067\u6307\u5b9a\u3055\u308c\u305f\u753b\u50cf\u30d1\u30b9\u3082\u5909\u66f4\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n<h2 class=\"wp-block-heading\">\u6ce8\u610f\u70b9<\/h2>\n\n\n<ul class=\"wp-block-list\">\n\t<li>\u3053\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u306f\u3001\u30da\u30fc\u30b8\u8aad\u307f\u8fbc\u307f\u5f8c\u306b\u52d5\u7684\u306b\u8ffd\u52a0\u3055\u308c\u305f\u8981\u7d20\u306b\u306f\u5f71\u97ff\u3057\u307e\u305b\u3093\u3002<\/li>\n\n\n\t<li>JavaScript\u304c\u7121\u52b9\u5316\u3055\u308c\u3066\u3044\u308b\u74b0\u5883\u3067\u306f\u6a5f\u80fd\u3057\u307e\u305b\u3093\u3002<\/li>\n\n\n\t<li>\u5927\u91cf\u306e\u8981\u7d20\u3084\u8907\u96d1\u306a\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u304c\u3042\u308b\u5834\u5408\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u5f71\u97ff\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n<h2 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u3068\u672c\u756a\u74b0\u5883\u3067\u7570\u306a\u308b\u753b\u50cf\u30d1\u30b9\u3092\u7c21\u5358\u306b\u7ba1\u7406\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u53ef\u80fd\u306a\u9650\u308a\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u306e\u89e3\u6c7a\u3084\u3001\u30d3\u30eb\u30c9\u30d7\u30ed\u30bb\u30b9\u3067\u306e\u9759\u7684\u306a\u7f6e\u63db\u3092\u691c\u8a0e\u3059\u308b\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002\u72b6\u6cc1\u306b\u5fdc\u3058\u3066\u6700\u9069\u306a\u65b9\u6cd5\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u306f\u3058\u3081\u306b Web\u30b5\u30a4\u30c8\u958b\u767a\u306b\u304a\u3044\u3066\u3001\u753b\u50cf\u30d1\u30b9\u3092\u52d5\u7684\u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u751f\u3058\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u306f\u3001\u30c6\u30b9\u30c8\u74b0\u5883\u3068\u672c\u756a\u74b0\u5883\u3067\u753b\u50cf\u30d1\u30b9\u304c\u7570\u306a\u308b\u5834\u5408\u306b\u3001JavaScript\u3092\u4f7f\u7528\u3057\u3066\u52d5\u7684\u306b\u753b\u50cf\u30d1\u30b9\u3092\u5909\u66f4\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002 \u306a\u305c[...]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","footnotes":"","vk-ltc-link":"","vk-ltc-target":"0"},"categories":[12],"tags":[312,313,314,113,115],"class_list":["post-5720","post","type-post","status-publish","format-standard","category-javascript","tag-312","tag-313","tag-314","tag-javascript","tag-dom"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/5720","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=5720"}],"version-history":[{"count":0,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/5720\/revisions"}],"wp:attachment":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media?parent=5720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/categories?post=5720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/tags?post=5720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}