I have a React component that displays and Iframe and I’m inserting into this Iframe <script>
element:
const iframe = ref?.current;
const iframeWindow = iframe?.contentWindow;
const iframeDocument = iframeWindow?.document;
if (!iframeDocument) return;
const iframeHead = iframeDocument.getElementsByTagName('head')[0];
const scriptElement = iframeDocument.createElement('script');
scriptElement.setAttribute('type', 'text/javascript');
scriptElement.innerText = ???; // Insert content of iframe.js here
iframeHead.appendChild(scriptElement);
I also have the .js file in the same component directory:
- iframe-wrapper
- IframeWrapper.tsx
- iframe.js // Import content from this file
- index.ts
I dont want to have the js content stored and imported as const
string. How can I get the content of iframe.js and set it as content of the <script>
element?