Skip to content

Instantly share code, notes, and snippets.

@rotexdegba
Last active December 12, 2017 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rotexdegba/8ae20c52e4c9a4590c49b13758b187f8 to your computer and use it in GitHub Desktop.
Save rotexdegba/8ae20c52e4c9a4590c49b13758b187f8 to your computer and use it in GitHub Desktop.
How to execute a JavaScript function when I have its name as a string
// see also https://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string
window.example = function () { alert('hello world'); };
//or
name = 'example';
window[name] = function () { alert('hello world'); };
//or
window[name] = new Function( 'alert("hello world")' );
//and finally call the function
example();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment