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

23
LINES

< > BotCompany Repo | #1032646 // js_executeScriptElements - better version of nodeScriptReplace?

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (32L/1K).

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

sS js_executeScriptElements() {
  ret [[
    function executeScriptElements(containerElement) {
      const scriptElements = containerElement.querySelectorAll("script");
    
      Array.from(scriptElements).forEach((scriptElement) => {
        const clonedElement = document.createElement("script");
    
        Array.from(scriptElement.attributes).forEach((attribute) => {
          clonedElement.setAttribute(attribute.name, attribute.value);
        });
        
        clonedElement.text = scriptElement.text;
    
        scriptElement.parentNode.replaceChild(clonedElement, scriptElement);
  });
}
  ]];
}

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: #1032646
Snippet name: js_executeScriptElements - better version of nodeScriptReplace?
Eternal ID of this version: #1032646/1
Text MD5: 9f82bc3c95d4e9b76e58a0f68173f729
Transpilation MD5: 389295c7189cc82b502a34239aaecefa
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:47:57
Source code size: 856 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 156 / 205
Referenced in: [show references]