Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Question about Javascript Variables

Reply
Thread Tools

Question about Javascript Variables

 
 
newspost2000
Guest
Posts: n/a
 
      02-23-2006
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,locat ion,height=400,width=500,top=26,left=280")
bookWindow.focus()
}
function msiWindow(webpage) {
bookWindow = window.open(webpage, "msiWin",
"menubar,status,scrollbars,resizable,toolbar,locat ion,height=600,width=754,top=0,left=0")
bookWindow.focus()
}
// -->

 
Reply With Quote
 
 
 
 
Jonas Raoni
Guest
Posts: n/a
 
      02-23-2006
newspost2000 wrote:
> 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;
}


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      02-23-2006

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


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

<script type = "text/javascript">

> <!--


HTML comment delimiters are unnecessary.

> function newWindow(webpage) {
> bookWindow = window.open(webpage, "newWin",
> "menubar,status,scrollbars,resizable,toolbar,locat ion,height=400,width=500,top=26,left=280")
> bookWindow.focus()
> }
> function msiWindow(webpage) {
> bookWindow = window.open(webpage, "msiWin",
> "menubar,status,scrollbars,resizable,toolbar,locat ion,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.

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      02-23-2006
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,loca tion,height=400,width=500,top=26,left=280")
>bookWindow.focus()
>}
>function msiWindow(webpage) {
>bookWindow = window.open(webpage, "msiWin",
>"menubar,status,scrollbars,resizable,toolbar,loca tion,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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
newbie question about scope, variables, declarations of variables and option strict (as in perl) Talha Oktay Ruby 8 03-08-2006 04:11 PM
question about class variables and instance variables Eric D. Ruby 3 02-01-2006 07:57 PM
Help with Javascript: naming variables using other variables sagejoshua HTML 16 11-09-2005 12:27 PM
Re: Class public shared Variables vs. Application Variables in ASP.NET avnrao ASP .Net 0 05-07-2004 05:28 AM



Advertisments