Perhaps using Web Workers . You could run your script, which you would like to enter as an isolated environment, so it will not ruin your current page.
As you said, it is possible that setAppLogo will be global inside "test/testApp_1.js" , so I will rely on this statement.
In the original script, you must create a working one that references the working script file and listen for the messages that will be received from the working one:
var worker = new Worker('worker.js'); worker.onmessage = function (e) {
Then, in the worker ( worker.js ), you can use the special function importScripts ( docs ), which allows you to load external scripts in the worker, the worker can also see the global variables of these scripts. There is also a postMessage function, available in working order to send user messages back to the original script, which in turn listens for these messages ( worker.onmessage ). Code for worker.js :
importScripts('test/testApp_1.js'); // "setAppLogo" is now available to worker as it is global in 'test/testApp_1.js' // use Worker API to send message back to original script postMessage(setAppLogo());
When called, you will get the result of setAppLogo in the listener:
worker.onmessage = function (e) { console.log(e.data);
This example is very simple, but you should learn more about the web workers API and possible errors.
Michael radionov
source share