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

268
Views
NodeJS tell open(html) to go first/tell other code to go last

I'm trying to determine if nodeJS is a viable solution for a work project at my internship. TLDR: I need to have my basicNode.js file wait until the open('./basic.html') has actually fully opened up the browser and loaded content before it calls any other code, either from basicNode.js itself or from change.js.

End goal/purpose of basicNode.js should be to open the html, and once it's loaded, call the changeThis() function from change.js to alter the html to show "Hello" instead of "Welcome to JavaScript". I've tried mixing up the order of things or having some console.logs output current time etc before calling the change functions but I just can't seem to get open('./basic.html') to fully complete before any attempt is made to change html elements

  1. basic.html
<!DOCTYPE html>
<html>  
<head>  
</head>  
<body>
<p>Date/Time: <span id="datetime"></span></p>
  
<p id="here">Welcome to JavaScript</p>  
<form>  
<input type="button" value="click" onclick="changeThis()"/>  
</form>  
</body> 
<script>
var dt = new Date();
    document.getElementById("datetime").innerHTML = dt.toLocaleString();
</script>
<script type="text/javascript" src="change.js"></script> 
<!--
<script>waitForElementToDisplay("#here",function(){alert("Hi");},1000,9000);</script> 
--> 
</html> 
  1. change.js
function changeThis() {
    document.getElementById("here").innerHTML = "Hello";
}

function waitForElementToDisplay(selector, callback, checkFrequencyInMs, timeoutInMs) {
  var startTimeInMs = Date.now();
  (function loopSearch() {
    if (document.querySelector(selector) != null) {
      callback();
      return;
    }
    else {
      setTimeout(function () {
        if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
          return;
        loopSearch();
      }, checkFrequencyInMs);
    }
  })();
}

function sendAlert() {
    alert("Hi");
}

module.exports={sendAlert};
module.exports={changeThis};
module.exports={waitForElementToDisplay};
  1. basicNode.js
const open = require('open');
var { change }  = require('./change')
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
global.document = new JSDOM('./basic.html').window.document;

open('./basic.html');

change.waitForElementToDisplay("#here",change.sendAlert(),1000,9000);
change.changeThis();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you're asking why it appears that open isn't "going first," open('./basic.html') is going first, it's just an asynchronous operation and returns a Promise. This is sort of an advanced topic for someone who is new to JS and Node, but if you want to run the change script after you've definitely opened the file, you'll need to await the promise. You can read all about this in the link to MDN above, and there is a ton of material online about this topic.

const open = require('open');
var { change }  = require('./change')
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
global.document = new JSDOM('./basic.html').window.document;

const main = async () => {
  await open('./basic.html');
  change.waitForElementToDisplay("#here",change.sendAlert(),1000,9000);
  change.changeThis();
}

main();

Note that I've wrapped the code in an async function because, for the moment, you can't use async/await syntax in the global scope. Alternatively, you could do as @JSmart523 indicated:

const open = require('open');
var { change }  = require('./change')
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
global.document = new JSDOM('./basic.html').window.document;


open('./basic.html').then(() => {
  change.waitForElementToDisplay("#here",change.sendAlert(),1000,9000);
  change.changeThis();
});
about 4 years ago · Juan Pablo Isaza 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!