I'm running an event listener on an object like below
googletag.pubads().addEventListener('slotOnload', (event) => {
// do something
});
What I'm looking for is load googletag.pubads()
via a variable value, and then the event listener should be applied on the variable
. For eg:-
var x = 'googletag.pubads';
x().addEventListener('slotOnload', (event) => {
// do something
});
Inspired from this answer, This is what I tried
function runFunction(name, arguments)
{
var fn = window[name];
if(typeof fn !== 'function')
return;
fn.apply(window, arguments);
}
var x = runFunction('googletag.pubads', []) // this returns undefined
For the uninitiated, googletag
is a package that shows ads on your page. Stackoverflow also uses googletag
for displaying the ads. You may try the eventListener
on Stackoverflow page console itself.
How can I fix this?? What am I doing wrong?