-2

I'm adding a script dynamically in my react application and when I try to get its src I got undefined. All I want is to get an empty string src. here's my sample code

 const script = document.createElement('script');
 script.id = 'sitepalBlock';
 script.type = 'text/javascript';
 script.innerHTML = `SomeHeavyComputionFunctionCall()`;
 document.getElementById('embed')?.appendChild(script);

if i try to add src mannualy like script.src="" then SomeHeavyComputionFunctionCall function not executing .

Asad Gulzar
  • 99
  • 1
  • 6
  • 2
    The question is unclear. Do you want to load a JavaScript file and then call a method defined in the file? – Yogi May 29 '22 at 08:40
  • I'm just creating script and call a function in that scripts inner html and that function is part of window object. but the issue it when i try to get the src of that script it has undefined value. e.g document.getElementById('sitepalBlock').src. // undefined – Asad Gulzar May 29 '22 at 10:04
  • I just want to avoid that undefined value – Asad Gulzar May 29 '22 at 10:05
  • 2
    "*I'm adding a script dynamically in my react application*", you're adding it dynamically, but when? maybe it has something to do with *when are you trying to write to the src attribute* and *when you're reading it*. my apologies but your question is incomplete, at least provide some code or a code sandbox sample to the problem you're trying to solve – Normal May 29 '22 at 10:23
  • 1
    Does this answer your question? [Adding script tag to React/JSX](https://stackoverflow.com/questions/34424845/adding-script-tag-to-react-jsx) – Yogi May 29 '22 at 10:35

0 Answers0