Created
August 13, 2016 04:18
-
-
Save JLChnToZ/c246d93c758bfbee621200dd73d37b90 to your computer and use it in GitHub Desktop.
Similar to eval(), but it is safe to be called in strict mode and will run the code asynchronously (Nothing will be returned).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Similar to eval(), but it is safe to be called in strict mode and will | |
| // run the code asynchronously (Nothing will be returned). | |
| // Available only in HTML5 DOM. | |
| function runScript(script) { | |
| if(!script) return; | |
| // Create a temporary blob that contains the script string and | |
| // add a script tag at the end of the document to contain that blob. | |
| var blob = new Blob([script.toString()], { type: 'text/javascript' }); | |
| var url = URL.createObjectURL(blob); | |
| var container = document.createElement('script'); | |
| container.src = url; | |
| document.body.appendChild(container); | |
| // The code should be runned asynchronously now, | |
| // we should GC the tempory created stuffs in here. | |
| URL.revokeObjectURL(url); | |
| document.body.removeChild(container); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment