Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
How to listen for variable to be declared|defined|available

How to 'listen' for the event that a variable has been declared/created?

From CSS I know that I can fire upon availibility of a CSS class like this:

let logo = document.querySelector('.test');
logo.addEventListener('load', (event) => {
  console.log('CSS class .test is defined now!');
});

Analogously to that I am looking for a 'querySelector' for variables, something like:

logo = window.hasOwnProperty('window.myVariable');
logo.addEventListener('load', (event) => {
  console.log('window.myVariable is defined now!');
});

Usage: I want to fire code as soon as an externally async loaded script has established the variable in question.

EDIT: some background In my web page (code I controll) I am loading a 3rd party .js script from another domain (code I don't controll). That script than loads more code from its domain. This 2nd script establishes a variable 'window.something { "key":"value",...} and then issues the loading of a third script. I want to interact with my js code to patch the variable as soon as it is available and before the 3rd script is executed. @reyno suggested the use of Proxy. My latest approach is to establish the variable myself and then watch for changes using Proxy.

<body>
  <script>
    // establish variable 
    window.someVariable={"someKey":"someValue"};
    
    // set up Proxy listener:
    var targetProxy = new Proxy(window.someVariable,{
      set: function (target, key, value) {
        console.log(`${key} set to ${value}`);
        target[key] = value;
        // change occured, thus 3rd party set values
        // ... add more code here ...
        return true;
      }
    });
    
    // test:
    console.log(window.someVariable);
    window.someVariable.someKey="NEW CONTENT";
    console.log(window.someVariable);
  </script>

  <!-- load the external script -->
  <script src="https://"></script>

  <!-- rest of html page ... -->

</body>

The problem now is that the other software, running in its own pop up window, looks like having its own scope, an 'own' window.someVariable, not overwriting mine.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First, set a custom event listener, I like to call it "notify".

document.addEventListener("notify", function() {
   console.log('window.myVariable is defined now!');
})

In your script that will load later, trigger this event listener when your variable is created.

const myVariable = "hello world";
document.dispatchEvent(new Event("notify"));

Example: https://jsfiddle.net/NHerwich/8b4gedxy/

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!