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).

1  
// from https://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml/69190644#69190644
2  
// fixes <script> tags in inserted HTML (e.g. via innerHTML)
3  
// TODO: Do we handle <script src=""> correctly?
4  
5  
sS js_executeScriptElements() {
6  
  ret [[
7  
    function executeScriptElements(containerElement) {
8  
      const scriptElements = containerElement.querySelectorAll("script");
9  
    
10  
      Array.from(scriptElements).forEach((scriptElement) => {
11  
        const clonedElement = document.createElement("script");
12  
    
13  
        Array.from(scriptElement.attributes).forEach((attribute) => {
14  
          clonedElement.setAttribute(attribute.name, attribute.value);
15  
        });
16  
        
17  
        clonedElement.text = scriptElement.text;
18  
    
19  
        scriptElement.parentNode.replaceChild(clonedElement, scriptElement);
20  
  });
21  
}
22  
  ]];
23  
}

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: 158 / 207
Referenced in: [show references]