skip to Main Content

In order to be GDPR compliant I wrapped all affiliate scripts on our webshop inside functions and add them to a job queue.

After the user accepts our privacy policy I run all queued jobs. I want to run them in global scope so all declared variables are accessible in global scope because some affiliate scripts are depending on global variables (I know that’s ugly 😉 ).

I know, I could rewrite all the javascript code and declare the varibales in global scope and wrap the rest of the code inside a function.
But then I would need to edit a lot of external modules (we are running a magento webshop and use external modules for including the affiliate scripts)

my current approach:

var jobQueue = [];
var add = function (fn) {
    jobQueue.push(fn);
    console.log("function pushed to queue");
};
var execute = function () {
    while ((curJob = jobQueue.pop()) !== undefined) {
        curJob();
        console.log("executed job");
    }
};

my problem is that some external scripts are depending on the variables declared inside my jobs. Is there any possibility to run the functions like the code was run globally?

I already found something like this but I could not make it work:
Javascript eval on global scope?
(this does not work with functions)

eval.call(window, x, y, z)

2

Answers


  1. Chosen as BEST ANSWER

    I found a possible solution but it is ugly:

    I save the body of the function (How to get function body text in JavaScript?) to a string and evaluate it in window scope

    var execute = function () {
        while ((curJob = jobQueue.pop()) !== undefined) {
            var entire = curJob .toString(); 
            var body = entire.slice(entire.indexOf("{") + 1, entire.lastIndexOf("}"));
            eval.call(window, body);
            console.log("executed job");
        }
    };
    

  2. Variables in Javascript have ‘function scope’, meaning that if you declare a variable inside a function, it won’t be accessible from outside that function.
    But when a variable doesn’t exist inside a function, Javascript will look for that variable outside that function until it reaches the global object.

    So variables declared in the global object are visible everywhere.

    You can assign variables to the global scope just by declaring them in the global scope.

    var pushing = "function pushed to queue";
    var executed = "executed job";
    var jobTitle = "... programing ...";
    
    var jobQueue = [];
    var add = function (fn) {
        jobQueue.push(fn);
        console.log( pushing );
    };
    var execute = function () {
        while ((curJob = jobQueue.pop()) !== undefined) {
            curJob();
            console.log( executed );
        }
    };
    
    // Lets add a Job
    add( function(){ console.log( jobTitle ); } );
    
    execute();
    

    JS Bin

    As you can see, the job added has access to the variable jobTitle that is declared in the global scope.

    Hope it helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search