Rich-text Reply

Custom Events Triggered On Window

BrookingsDev 01-26-17

Custom Events Triggered On Window

I have an issue where my code works fine when executed trough the console, but once it is executed via an experiment, my function 'showNewsletter' do not execute... only sometimes on Safari and Firefox, and never on Chrome...



window.addEventListener('scroll', showNewsletter);


And idea?

Michal 01-30-17

Re: Custom Events Triggered On Window

Hi there,

This is a bit hard without further context. Are you getting any errors when your code is loaded as a part of Optimizely? Also, can you please let me know where exactly within Optimizely are you using this and what are you trying to achieve?

Looking forward to hearing back from you!

RyanC 02-01-17

Re: Custom Events Triggered On Window

You would want to create a setinterval until showNewsletter function is available on the page, before running the above script.
The best solution is the simplest.
Experimentation Hero
JasonDahlin 02-02-17

Re: Custom Events Triggered On Window

In Optimizely X there is a built in function for waiting for something to exist.

In Classic, you can use this:

/* _optimizely_evaluate=force */
function waitForDelayedContent(selector, experiment, timeout, keepAlive) {
    var intervalTime = 25;
    var timeout = timeout || 3000;
    var keepAlive = keepAlive || false;
    var maxAttempts = timeout / intervalTime;
    var attempts = 0;
    var interval = setInterval(function() {
        var selectorVal = (function(){
            try {
                return eval(selector);
            } catch(e) {
                return undefined;
        if (typeof selectorVal !== 'undefined') {
            if (!keepAlive) {
        } else  if (attempts > maxAttempts) {
        attempts ++;
    }, intervalTime);

waitForDelayedContent("showNewsletter", function(){
  window.addEventListener('scroll', showNewsletter);
/* _optimizely_evaluate=safe */



--Jason Dahlin
Analytics and Testing Guru Smiley Happy

Experimentation Hero