Newbie:What is this in the code?

P

Patrick

Hi

I am in the process of learning JS.This particular portion of code is
part of an online tutorial about the open() and close() methods of the
window object.
I like to have found out how to access newly openened windows and
being able to change their properties via the parent window.
But what exactly is that "aa" in there?
I understand it is a name given to the newly open window but i am
confused as if it is a variable or something else?
I thought the name of the new window would be between the URL and
properties
('URL','Newwindownamehere','properties') and that this name('New
windownamehere') would be used to access the new window from the
parent window.
I tried to put var in front of 'aa' and got an error.Obviously since
that code isn't between the <SCRIPT></SCRIPT> tags, i doubt 'aa' is a
variable.
I guess as i keep learning objects, i will find out but it's bugging
me!
So i hope someone can tell me.Beside that i do understand all the code
pasted here.


<body>
<form>
<input type="button" value="Open another page"
onClick="aa=window.open('test.htm','','width=200,height=200')">
<input type="radio" name="x" onClick="aa.document.bgColor='red'">
<input type="radio" name="x" onClick="aa.document.bgColor='green'">
<input type="radio" name="x" onClick="aa.document.bgColor='yellow'">
</form>
</body>

Thanks a lot

Patrick
(e-mail address removed)
(The posting email isn't valid,use the one above.)
 
B

Bryan Field-Elliot

I can see how this would be confusing. The variable which is returned
(named "aa" in your sample) is what you would use later in your
javascript to operate on the new window.

The middle parameter to the "open" command (which is called the window
name), is never used by Javascript. It is only useful when you later
have a hyperlink (<A> tag) with a TARGET attribute. If you need to
target another window for hyperlink, use the window name
("Newwindownamehere").
 
G

Graham J

But what exactly is that "aa" in there?
I understand it is a name given to the newly open window but i am
confused as if it is a variable or something else?

It is a variable. The window.open function returns a window object and that
needs to be allocated to a variable so it can be referenced in the other
onClick functions.
I thought the name of the new window would be between the URL and
properties ('URL','Newwindownamehere','properties') and that this name('New
windownamehere') would be used to access the new window from the
parent window.

The 'Newwindownamehere' is the name that can be used as a 'target' in HTML
e.g. said:
I tried to put var in front of 'aa' and got an error.Obviously since
that code isn't between the <SCRIPT></SCRIPT> tags, i doubt 'aa' is a
variable.

You will also get an error if you click on a radio button before opening the
window. It is the same error, which is that 'aa' is undefined.

Your code at the moment effectively equates to something like this...

function onClick1()
{
aa=window.open('test.htm','','width=200,height=200')";
}

function onClick2()
{
aa.document.bgColor='red'";
}

function onClick3()
{
aa.document.bgColor='green';
}

function onClick4()
{
aa.document.bgColor='yellow';
}

When you click on the button to open the window the variable 'aa' is
automatically defined as a global variable for you. This can then be used
in any of the functions in the document. If you add the 'var' in the
onClick function you effectively do...

function onClick1()
{
var aa=window.open('test.htm','','width=200,height=200')";
}

....which limits the scope of that variable to the onClick function so the
other functions cannot see it.

What you really want is

<script type='text/javascript'>
var aa;
</script>

in the head of your document or defined in the body before the form. Then
you will have defined 'aa' as a variable before it is used. Effectively...

var aa;

function onClick1()

etc etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top