Using page-specific <script> element with Turbo Frames

Hi.

I’m new and apologies if this has been asked before. I found one posting, but I’m not sure if it’s about the same, and it has no reply:

I’m using Turbo Frames, and there’s a script element used only for one page, so rather than including it in the top-level element, I’d like to add it as part of navigating to that page.

According to the manual, this is supposed to be possible, but I’m not sure how. I’ve tried several ways, such as:

<html>
  <head>
    <script src="..."></script>
  </head>
  <body>
    <turbo-frame id="...">
      ...
    </turbo-frame>
  </body>
</html>

and:

<turbo-frame id="...">
  <html>
    <head>
      <script src="..."></script>
    </head>
    <body>
      ...
    </body>
  </html>
</turbo-frame>

However, none of them load the script.

Any help appreciated.

Regards,

Terje

I believe it should work if you have the <script> tag inside the <turbo-frame> but not inside any <head> tag. Like:

<html>
  <body>
    <turbo-frame id="...">
      ...
      <script src="..."></script>
    </turbo-frame>
  </body>
</html>

Thanks for your reply.

I thought so too, that’s how I had it originally. If you have a <script> element with JavaScript inside of it, that script will get executed every time you load the page.

However, it appears that if you link to a script from such an element it doesn’t.

Ahh, I didn’t catch that you were linking to a script, not executing a given script body. I’m not sure, I have never tried that in a Turbo-Frame.