A question with a JavaScript variable variable: for var, or not for var

Thank you very much in advance. I work from a tutorial, and they use one function to call another, which opens a window:

function rtest(){
   content='dans window';
   oneWindow=open("","Window 1","width=450,height=290");
   newWindow(oneWindow);
}
function newWindow(x){
   x.document.close();
   x.document.open();
   x.document.write(content);
   x.document.close();
   x.moveTo(20,20);
   x.focus();
}

So, everything works fine, but my question is this: how can the newWindow () function access the contents of the “content” in the rtest () function? And why, if I preface the variable "content" with "var", like this:

function rtest(){
  **var content='dans window';**
  oneWindow=open("","OneWindow","width=450,height=290");
  newWindow(oneWindow);
}

... an error occurs (and the contents of the new window remain empty)?

Can someone explain to me what the difference is between using var and not using var?

Thank!

Daniel

+3
source share
5 answers

var , .

, . . , . , ( ) .:-) , ( ) , 200 .

:

var content;

function rtest(){
    content='dans window';
    oneWindow=open("","Window 1","width=450,height=290");
    newWindow(oneWindow);
}

, - open("about:blank", ...), open("", ...).

+5

var rtest, . javascript-, newWindow. , var, rtest, , , .

+6

-, var, , .

var ( ), ​​ Global.

+2

var, ; , . var, , , scope, ( , ).

, , , . , ; , , . , , , , , .

+2

var , , , . var . Javascript . , window.content = 'dans window'; , , . : , .

+1

All Articles