Quantcast
Channel: How to execute a JavaScript function when I have its name as a string - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Answer by Magnus Smith for How to execute a JavaScript function when I have its name as a string

$
0
0

I can't resist mentioning another trick, which helps if you have an unknown number of arguments that are also being passed as part of the string containing the function name. For example:

var annoyingstring = 'call_my_func(123, true, "blah")';

If your Javascript is running on a HTML page, all you need is an invisible link; you can pass a string into the onclick attribute, and the call the click method.

<a href="#" id="link_secret"><!-- invisible --></a>

$('#link_secret').attr('onclick', annoyingstring);$('#link_secret').click();

Or create the <a> element at runtime.


Viewing all articles
Browse latest Browse all 38

Trending Articles