-2

I am running a local server as shown and when I load it, it says "window not defined" what could be the problem here? I am waiting till it loads my index.html file and calling a callback to it.

module.exports=function(){
 //require the express module to use it in the app
 var express=require('express');
 //create an express app, fire the express function
 //to be able to use the methods in express
 var app=express();
app.listen(3000);
app.get('/', function(req,res){
res.sendFile(__dirname+'/index.html',function(){
  window.onload=function(){
    alert('webpage loaded');

    }
  });
 });
}
akotch
  • 215
  • 5
  • 11
  • I looked at the browser and it says window{onload:null } – akotch Jul 15 '18 at 17:19
  • @andrewL the title sounds promising but the question isnt really related (there is a gpod explanation of this case in PHP but I'm unable to find it) – Jonas Wilms Jul 15 '18 at 17:23

1 Answers1

4

Your javascript runs on the server. The server has no window property. Add clientside code to your html, so it will be sent to the client by the nodejs' javascript and then it can execute on the client:

<script>
  window.onload = function() { /*...*/ };
</script>

Read on

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151