Question about Javascript Variables

N

newspost2000

Hi...

I was just reviewing some javascript pop up window functions on my
website and I descovered something... that I set the exact same
variable name twice for both functions... which is bookWindow. I
tested this for pages that use both functions and both types of pop
windows work no problem. Is there a problem in that I use the same
variable name twice? Is this considered bad form? Or maybe it makes
not difference at all because the variable only gets used when it is
called upon and then it leave memory.

All help and insite is appreciated.


<script language="JavaScript" type="text/javascript">
<!--
function newWindow(webpage) {
bookWindow = window.open(webpage, "newWin",
"menubar,status,scrollbars,resizable,toolbar,location,height=400,width=500,top=26,left=280")
bookWindow.focus()
}
function msiWindow(webpage) {
bookWindow = window.open(webpage, "msiWin",
"menubar,status,scrollbars,resizable,toolbar,location,height=600,width=754,top=0,left=0")
bookWindow.focus()
}
// -->
 
J

Jonas Raoni

newspost2000 said:
I was just reviewing some javascript pop up window functions on my
website and I descovered something... that I set the exact same
variable name twice for both functions...
[...]

Is there a problem in that I use the same
variable name twice? Is this considered bad form?

There's no problem, but the variable value will be replaced.

You call function A:
X = A
After calling function B:
X = B

If you're not going to use that variable, I suggest you to declare it
using "var", so it won't be acessible out of function.

function getWindow(bla, bla, bla){
var w = window.open(....
:
return w;
}
 
W

web.dev

newspost2000 said:
<script language="JavaScript" type="text/javascript">

The language attribute is deprecated, just stick with the type
attribute:


HTML comment delimiters are unnecessary.
function newWindow(webpage) {
bookWindow = window.open(webpage, "newWin",
"menubar,status,scrollbars,resizable,toolbar,location,height=400,width=500,top=26,left=280")
bookWindow.focus()
}
function msiWindow(webpage) {
bookWindow = window.open(webpage, "msiWin",
"menubar,status,scrollbars,resizable,toolbar,location,height=600,width=754,top=0,left=0")
bookWindow.focus()
}

You could _potentially_ have a problem. Since you have not declared
bookWindow with a "var", the variable is not local to the function.
You are using bookWindow as a global variable, thus you might overwrite
a value that you have assigned earlier. It is generally good form to
declare variables using the var keyword.
 
L

Lee

newspost2000 said:
Hi...

I was just reviewing some javascript pop up window functions on my
website and I descovered something... that I set the exact same
variable name twice for both functions... which is bookWindow. I
tested this for pages that use both functions and both types of pop
windows work no problem. Is there a problem in that I use the same
variable name twice? Is this considered bad form? Or maybe it makes
not difference at all because the variable only gets used when it is
called upon and then it leave memory.

All help and insite is appreciated.


<script language="JavaScript" type="text/javascript">
<!--
function newWindow(webpage) {
bookWindow = window.open(webpage, "newWin",
"menubar,status,scrollbars,resizable,toolbar,location,height=400,width=500,top=26,left=280")
bookWindow.focus()
}
function msiWindow(webpage) {
bookWindow = window.open(webpage, "msiWin",
"menubar,status,scrollbars,resizable,toolbar,location,height=600,width=754,top=0,left=0")
bookWindow.focus()
}
// -->

The variable never leaves memory. Since you don't declare it as
local to the function, it is global. Since you don't use it for
much, it's doing no harm. It would be better to declare it as
local (just add the "var" keyword, as in: var bookWindow = ...),
or you could eliminate it altogether by using:
window.open(webpage,...left=0").focus();
or you could combine your two functions into one that takes the
window name, size and location as arguments.

Since you're interested in good form, the "language" attribute
of the script tag is deprecated and the SGML comments (-->, //-->)
don't serve any purpose anymore.
 

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

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,297
Latest member
EngineerD

Latest Threads

Top