Last active
December 12, 2017 20:55
-
-
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
This file contains 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
// 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