Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    55
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Variable naming convention - newbie question

    Just wondering, if I open 2 different 'popup windows', both run via 2 different javascript functions, and they both use the same name 'newwindow' (for example), could that eventually cause problems or runtime errors?

    Should I be giving these 2 different names or does it really matter.

    Thanks guys.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    ??? What does the name of the window have to do with a variable name?

    Or do you mean you do:
    Code:
    var newwindow = window.open( ... );
    twice?

    If that variable is a PAGE SCOPE (a.k.a. global) variable, then the second usage overwrites the first one.

    If that variable is defined in a function, then it has scope only in that function. As soon as the function ends, the variable disappears.

    Maybe you need to show some code???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    "newwindow" isn't a particularly good name either way -- rather choose a name that actually describes the window. Then, if your two windows are actually windows with different content, the names will differ naturally.

    Also, it is very common to use camel case on variable names, i.e. newWindow instead of newwindow.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    Although it is possible to have two local variables in different functions with the same name, this is generally a bad idea, and is likely to lead to confusion. Exceptions might be loop counters i, j, k etc. but even then it is important to use the var keyword to ensure that these variables are local to the respective functions. Leaving that out can lead to weird and unpredictable outcomes.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    most browsers won't let you launch two popups.
    if you use the same name (2nd argument) for both in the window.open() command, the 2nd one will take over the first, and you will have only one.

    this is the same as repeating the name later, even after the top page reloads, re-using the name will automagically re-bind to the already-opened popup.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by rnd me View Post
    most browsers won't let you launch two popups.
    if you use the same name (2nd argument) for both in the window.open() command, the 2nd one will take over the first, and you will have only one.
    Which is why I don't think that's what he was asking about.

    He said he used the same name twice and it worked. So I think he really did mean variable name.

    But who knows. He won't come back and clarify his question.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by Old Pedant View Post
    He said he used the same name twice and it worked. So I think he really did mean variable name.
    no he didn't: "if I open 2 different 'popup windows', ... "...

    the name is the 2nd argument to window.open(), not the objectWindow return value of window.open().

    you can re-assign different popups all day on one assignment var, it's the name of the window (arg2) that has distinct behavior upon recycling...

    it's been a long time since multiple popups have been allowed, even in IE.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by rnd me View Post
    it's been a long time since multiple popups have been allowed, even in IE.
    Could you quote a source for this statement? I don't find that advice anywhere else. What is wrong with

    Code:
    <script type="text/javascript">
    
    function openmany(a,b,c){
    window.open(a,'test1');
    window.open(b,'test2');
    window.open(c,'test3');
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •