question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scripts do not execute when added.

See original GitHub issue

When a script is added to the page either through a script with a src attribute or an inline script it will not download the script or execute the inline code. Is this expected behavior?

Thank you!

Edit:

This does execute script tags which have a src attribute

{
    onNodeAdded: function (node) {
        if (node.nodeName === 'SCRIPT' && node.src) {
            var script = document.createElement('script');
            script.src = node.src;
            node.replaceWith(script)
        }
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

6reactions
logankeenancommented, Jul 1, 2020

The code below will execute JS code as it’s added/updated.

        let morphdomOptions = {
            onNodeAdded: function (node) {
                if (node.nodeName === 'SCRIPT') {
                    var script = document.createElement('script');
                    //copy over the attributes
                    [...node.attributes].forEach( attr => { script.setAttribute(attr.nodeName ,attr.nodeValue) })

                    script.innerHTML = node.innerHTML;
                    node.replaceWith(script)
                }
            },
            onBeforeElUpdated: function (fromEl, toEl) {
                if (fromEl.nodeName === "SCRIPT" && toEl.nodeName === "SCRIPT") {
                    var script = document.createElement('script');
                    //copy over the attributes
                    [...toEl.attributes].forEach( attr => { script.setAttribute(attr.nodeName ,attr.nodeValue) })

                    script.innerHTML = toEl.innerHTML;
                    fromEl.replaceWith(script)
                    return false;
                }
                return true;
            }
        };
1reaction
logankeenancommented, Jul 1, 2020

@snewcomer - thanks for your suggestion with replaceWith too! That worked perfectly! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically added script will not execute - Stack Overflow
But If I try to add it to the page after it loads it adds the script to the page but doesn't execute...
Read more >
How is it possible that a <script> tag was injected, but not ...
To execute the script, you have to create the script element via createElement and append it to the DOM explicitly. Share.
Read more >
18843 - dynamically added SCRIPT not executable
When I have added a SCRIPT element it shows up in the DOM but the inserted javascript is not defined. Press the insert...
Read more >
Adding arguments and options to your Bash scripts - Red Hat
Bash uses a tool called positional parameters to provide a means of entering data into a Bash program when it is invoked from...
Read more >
Using functions within a shell script
This code is not executed until the function is called. Functions are read in, but basically ignored until they are actually called.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found