Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript window is undefined

Here is some of my javascript:

(function(window) {
  window.file = {};
  file.i = 0;
  
  for(;;) {
     if(file.i++ >= 10) break;
     document.body.appendChild(document.createTextNode(file.i))
  } 
  
 
}) ();

Why is window undefined?

like image 967
chuck Avatar asked Apr 13 '26 14:04

chuck


1 Answers

You need to call the anonymous function with window as the first argument:

(function(window) {
  window.file = {};
  file.i = 0;
  
  for(;;) {
     if(file.i++ >= 10) break;
     document.body.appendChild(document.createTextNode(file.i))
  } 
}) (window);

Since you provided nothing, window inside of your function's scope was considered undefined.

like image 183
Mystical Avatar answered Apr 15 '26 05:04

Mystical