Skip to content

Instantly share code, notes, and snippets.

@JLChnToZ
Created August 13, 2016 04:18
Show Gist options
  • Select an option

  • Save JLChnToZ/c246d93c758bfbee621200dd73d37b90 to your computer and use it in GitHub Desktop.

Select an option

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