Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

39
LINES

< > BotCompany Repo | #1032645 // js_nodeScriptReplace (backup)

JavaX fragment (include)

// from https://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml
// fixes <script> tags in inserted HTML (e.g. via innerHTML)
// TODO: Do we handle <script src=""> correctly?

sS js_nodeScriptReplace() {
  ret [[
    function nodeScriptReplace(node) {
      if (nodeScriptIs(node)) {
        node.parentNode.replaceChild(nodeScriptClone(node), node);
        console.log("nodeScriptReplace finished replacement");
      } else {
        var i = -1, children = node.childNodes;
        while (++i < children.length)
          nodeScriptReplace(children[i]);
      }
    
      return node;
    }
    
    function nodeScriptClone(node){
      var script  = document.createElement("script");
      if (node.src)
        console.log("nodeScriptReplace including " + node.src);
      else {
        console.log("nodeScriptReplace inline script");
        script.text = node.innerHTML;
      }

      var i = -1, attrs = node.attributes, attr;
      while ( ++i < attrs.length )
        script.setAttribute( (attr = attrs[i]).name, attr.value );
      return script;
    }
    
    function nodeScriptIs(node) {
      return node.tagName === 'SCRIPT';
    }
  ]];
}

Author comment

Began life as a copy of #1032354

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032645
Snippet name: js_nodeScriptReplace (backup)
Eternal ID of this version: #1032645/1
Text MD5: 1a321aaf92e9be7b29b1f67315c5d8d4
Author: stefan
Category: javax / javascript
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-30 02:45:12
Source code size: 1224 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 116 / 137
Referenced in: [show references]