Accessing document.currentScript.src inside a React component

1 hour ago 1
ARTICLE AD BOX

I want to access document.currentScript.src to get the domain of the current executing script for use in a React component.

When I try to access it like this, it works fine.

const scriptSrc = document.currentScript?.getAttribute("src") const MyComponent: React.FC<> = () => { const myUrl = scriptSrc + "filename"; }

But when I try to access it inside the React component, it almost always return null except for a few times.

const MyComponent: React.FC<> = () => { const scriptSrc = document.currentScript?.getAttribute("src") //returns null const myUrl = scriptSrc + "filename"; }

Can you explain why this is happening?

Read Entire Article