# カスタムJavaScriptはリフレッシュ後にのみ適用され、locationプラグインのカテゴリが作成されます

**URL:** <https://meta.discourse.org/t/custom-javascript-gets-only-applied-after-refresh-creating-categories-for-location-plugin/378365>\
**Category:** Development\
**Created:** [2025 年 8 月 12 日午後 10:44 UTC](https://meta.discourse.org/t/custom-javascript-gets-only-applied-after-refresh-creating-categories-for-location-plugin/378365 "2025-08-12T22:44:44Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![opcourdis](https://avatars.discourse-cdn.com/v4/letter/o/45deac/32.png) [@opcourdis](https://meta.discourse.org/u/opcourdis)\
**Post date:** [2025 年 8 月 12 日午後 10:44 UTC](https://meta.discourse.org/t/custom-javascript-gets-only-applied-after-refresh-creating-categories-for-location-plugin/378365/1 "2025-08-12T22:44:44Z")

</div>

こんにちは。

以下のスクリプトは、場所プラグインでツールチップを永続的に表示し、タイプ（イベント、会場など）に応じて並べ替えるために色を付けます。凡例とフィルターを検討中です。

状況は、それが機能するのですが、ページをリロードした後だけで、URLからページにアクセスしたときは機能しません。

両方のケースでコンソールにエラーの違いはなく、関数に遅延を追加しても何も変わりません。

テーマ編集メニューからヘッダーにJavaScriptを追加した際に、このような経験をした方で解決策を見つけた方はいらっしゃいますか？

以下は結果の画像です。

 ![image](https://global.discourse-cdn.com/meta/original/4X/3/3/9/339b487a8fa68391f58854b68b2c22fa4b24ab21.jpeg)

ここでのエラーはコードの有無にかかわらず表示されるため、コードとは関係ありません。

 ![Copie d'écran_20250813_001747](https://global.discourse-cdn.com/meta/original/4X/4/8/5/485585913ef5ac9540da221b4085433be4d8ee59.png)

```javascript
<script>
document.addEventListener('DOMContentLoaded', function() {
  // Apply CSS to change the alt text style
  var style = document.createElement('style');
  style.type = 'text/css';
  style.innerHTML = `
    /* Ensure alt text is bold, underlined, and has conditional color */
    .leaflet-marker-pane img[alt] {
      font-weight: bold !important; /* Make text bold */
      text-decoration: underline !important; /* Underline the text */
      white-space: nowrap; /* Prevent the text from wrapping to a new line */
    }
  `;
  document.head.appendChild(style);

  // Give the page a moment to load and the images to be rendered
  setTimeout(function() {
    var images = document.querySelectorAll('.leaflet-marker-pane img');

    images.forEach(function(img) {
      var title = img.getAttribute('title');
      if (title) {
        title = title.length > 10 ? title.substring(0, 10) + '..' : title;
        img.setAttribute('alt', title);
        img.setAttribute('title', title);

        if (title.includes('/')) {
          img.style.color = 'white';
        } else {
          img.style.color = 'black';
        }
      }
      img.setAttribute('src', '');
    });
  }, 1000); // Delay execution by 1 second (1000 ms)
});

</script>

```

---

_[View the full topic](https://meta.discourse.org/t/custom-javascript-gets-only-applied-after-refresh-creating-categories-for-location-plugin/378365)._
