Question about Javascript Variables

Discussion in 'Javascript' started by newspost2000, Feb 23, 2006.

  1. newspost2000

    newspost2000 Guest

    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()
    }
    // -->
     
    newspost2000, Feb 23, 2006
    #1
    1. Advertising

  2. newspost2000

    Jonas Raoni Guest

    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
     
    Jonas Raoni, Feb 23, 2006
    #2
    1. Advertising

  3. newspost2000

    web.dev Guest

    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,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.
     
    web.dev, Feb 23, 2006
    #3
  4. newspost2000

    Lee Guest

    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.
     
    Lee, Feb 23, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    0
    Views:
    3,369
    avnrao
    May 7, 2004
  2. sagejoshua
    Replies:
    16
    Views:
    662
    Michael Winter
    Nov 9, 2005
  3. Replies:
    9
    Views:
    959
  4. Eric D.
    Replies:
    3
    Views:
    180
    Jeremy Henty
    Feb 1, 2006
  5. Talha Oktay
    Replies:
    8
    Views:
    218
Loading...

Share This Page