From the docs....
Your browser automatically loads and evaluates any elements
present on the initial page load.
When you navigate to a new page, Turbolinks looks for any
elements in the new page’s which aren’t present on the current
page. Then it appends them to the current where they’re loaded
and evaluated by the browser. You can use this to load additional
JavaScript files on-demand.
Turbolinks evaluates elements in a page’s each time it
renders the page. You can use inline body scripts to set up per-page
JavaScript state or bootstrap client-side models. To install behavior,
or to perform more complex operations when the page changes, avoid
script elements and use the turbolinks:load event instead.
In practice, you can just have your script in the body tag.
If's it's anything more complex or perhaps by external form, you mean something like an iframe, I've done the following in the past for trustpilot. Granted, it's not a form but it might help you on your way.
In the head tag, I have put
<head>
<!-- TrustBox script -->
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>
<!-- End Trustbox script -->
</head>
Then in the body, I have a div
<div id='trustpilot' class="trustpilot-widget" data-locale="en-UK" data-template-id="53aa8912dec7e10d38f59f36" data-businessunit-id="58dd0d0a0000ff00059f84bc" data-style-height="130px" data-style-width="100%" data-theme="light" data-stars="1,2,3,4,5">
<a href="https://uk.trustpilot.com/review/company" target="_blank">Trustpilot</a>
</div>
Finally, I use a Turbolinks event listener to activate the iframe.
document.addEventListener("turbolinks:load", function () {
// find trustpilot header
var trustpilotHeader = document.getElementById('trustpilot-header');
if (trustpilotHeader) {
window.Trustpilot.loadFromElement(trustpilotHeader);
} else {
// handle element not found
return;
}
var trustpilot = document.getElementById('trustpilot');
if (trustpilot) {
window.Trustpilot.loadFromElement(trustpilot);
} else {
// handle element not found
return;
}
});