0

There is one use case of mine where I needed to dynamically append js to header. Is there any way we can append script tag with some js in lightning web component.

1

No, you can't do that. The framework will prevent your code from running. You must use loadScript in order to load scripts. Since the JavaScript is under your control, you can choose which of several scripts you'd like to add, but this must still be statically bound in a Static Resource.

0

Try this: Create an empty div and add this attribute-> lwc:dom="manual" you can then access the div from javascript using the Id/Class name and append the script.

The link below has a perfect example of how to achieve this:

appendChild to non empty div in lightning web component

Not the answer you're looking for? Browse other questions tagged or ask your own question.