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 12 of 12
  1. #1
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,865
    Thanks
    6
    Thanked 1,029 Times in 1,002 Posts

    Is eval() necessary here, and if yes, how?

    I’ve come across this ancient piece of JavaScript:
    PHP Code:
    <script language="JavaScript">
    function 
    popUp(URL) {
    day = new Date();
    id day.getTime();
    eval(
    "page" id " = window.open(URL, '" id "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=700,height=500,left = 162,top = 134');");
    }
    </script> 
    What’s the purpose of the eval() here? I also don’t see any other reference to “page” in the source code from where I go this (I doubt that it is used in any other external script). So, what’s the point and why couldn’t I just run window.open() by itself?

    The reason why I’m asking is because I want to update that script a little to validate in strict mode and JShint is complaining about eval().
    Last edited by VIPStephan; 11-29-2012 at 06:16 PM.

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    The second parameter of window open is optional.
    Specifies the target attribute or the name of the window. The following values are supported:
    • _blank - URL is loaded into a new window. This is default
    • _parent - URL is loaded into the parent frame
    • _self - URL replaces the current page
    • _top - URL replaces any framesets that may be loaded
    • name - The name of the window
    [S]Then the eval("page"+Id) could be replace by a window["page"+id][/S]
    Last edited by 007julien; 11-29-2012 at 07:38 PM.

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I assume it is used to create page1354211652677 as a global variable that references the new window. But the only way to then use it is to re-construct the variable name using eval again, and the global variable id.

    A way to avoid this would be to have a global array that stores the references to new windows - assuming popUp() is called more than once.

    The id (a number) is used to name the new window anyway.. It shouldn't be just a number (although this might still work..). But the name is not much use, other than setting it as a target for a link.
    Last edited by AndrewGSW; 11-29-2012 at 07:05 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by AndrewGSW View Post
    I assume it is used to create page1354211652677 as a global variable that references the new window.
    Presumably by someone with very little JavaScript knowledge or they'd have coded it properly as window["page"+id] (as 007julien already said in the prior post) - since eval is almost completely unnecessary in JavaScript with only a few advanced situations where it is actually needed. Beginner and intermediate JavaScript never needs eval().

    While eval() is still a valid command in strict JavaScript any variables defined inside the eval go out of scope when the eval finishes running which means it can no longer be used in situations such as this but can still be used in those advanced situations it is intended for.
    Last edited by felgall; 11-29-2012 at 07:32 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,865
    Thanks
    6
    Thanked 1,029 Times in 1,002 Posts
    Quote Originally Posted by AndrewGSW View Post
    The id (a number) is used to name the new window anyway.. It shouldn't be just a number (although this might still work..). But the name is not much use, other than setting it as a target for a link.
    Hm, I wouldn’t know if/how the windows are referenced in this application. That function is called from several links and repeatingly clicking the same link(s) just opens new windows. Could be that they meant to always open a new window by adding a timestamp and not re-using an already open one but that would happen automatically if no target/name is provided, I suppose. I have no idea. Maybe I’ll just take the risk and change that function altogether.

    Thanks for the info, guys.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by VIPStephan View Post
    That function is called from several links and repeatingly clicking the same link(s) just opens new windows.
    That means that the value in the second parameter of the window.open call isn't working. Where that value matches the name of a window that is already open then the page should open in that window rather than opening another window as you are not supposed to be able to have two windows with the same name.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,865
    Thanks
    6
    Thanked 1,029 Times in 1,002 Posts
    Quote Originally Posted by felgall View Post
    That means that the value in the second parameter of the window.open call isn't working. Where that value matches the name of a window that is already open then the page should open in that window rather than opening another window as you are not supposed to be able to have two windows with the same name.
    Well, I thought it is working because it creates a new date string everytime the link is clicked, and it is using that date string as the parameter value; echoing the window name in the console gives me a long number, apparently the Unix timestamp, which is different in each window.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by VIPStephan View Post
    Well, I thought it is working because it creates a new date string everytime the link is clicked, and it is using that date string as the parameter value; echoing the window name in the console gives me a long number, apparently the Unix timestamp, which is different in each window.
    Presumably there is subsequent processing that needs to reference those windows by those names - otherwise '_blank' would be a simpler alternative for that parameter.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I apologise in advance if I have misunderstood something .. but.. that function uses getTime() each time to name the window, which will be different on each occasion - so the same window will never be re-used.

    If, as felgall advises, the variable created by eval() immediately goes out of scope, then no reference to the (new) window is retained. As far as I understand.. neither window['name'] nor windows['name'] (there is no windows collection..) can be used to reference a window.

    (.. I don't think the variable created by eval goes out of scope..? But I await correction )

    Code:
    function init() {
        eval('x=12');
    }
    window.onload = init;
    
    <button onclick="alert(x);">Still there?</button>
    Added: So window["page"+id] could reference an existing window, but it is referencing it by means of the global variable named 'page123456..' and not the name of the window - although they happen to be the same in this instance.
    Last edited by AndrewGSW; 11-29-2012 at 11:50 PM. Reason: Added code
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    ..attempting to create a local variable with

    Code:
    eval('var x=12');
    is more complicated as (my research tells me) some browsers will hoist it to the global scope.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    So, to answer your question I suggest replacing

    Code:
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=700,height=500,left = 162,top = 134');");
    with

    Code:
    window["page" + id] = window.open(URL, id, 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=700,height=500,left = 162,top = 134');
    possibly changing the name (2nd argument) from id to "page"+id (so it isn't just a number).
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by AndrewGSW View Post
    (.. I don't think the variable created by eval goes out of scope..? But I await correction )
    It is one of the changes that was made in the ECMAScript 5 specification. When you use strict JavaScript any variables defined in an eval go out of scope when the eval finishes - there is nothing you can do that requires an eval and also requires that something defined there continue to exist after the eval finishes.

    The Mozilla page listing the changes describes it like this:

    eval of strict mode code does not introduce new variables into the surrounding scope. In normal code eval("var x;") introduces a variable x into the surrounding function or the global scope. This means that, in general, in a function containing a call to eval every name not referring to an argument or local variable must be mapped to a particular definition at runtime (because that eval might have introduced a new variable that would hide the outer variable). In strict mode eval creates variables only for the code being evaluated, so eval can't affect whether a name refers to an outer variable or some local variable:
    Code:
    var x = 17;
    var evalX = eval("'use strict'; var x = 42; x");
    assert(x === 17);
    assert(evalX === 42);
    Note that JavaScript also now requires that you declare all variables in the scope you intend to use them in so that mistyping a variable name or failing to declare one will not pollute the global namespace.

    eval('text' + varfld) can always be rewritten as window['text+ varfld] if it is outside a function. Inside a function called myfunc eval('text' + varfld) can always be rewritten as myfunc['text+ varfld]

    The only situation I have ever come across where an eval is necessary is inside a Function.prototype method that is dynamically reqriting all the functions in your page and there the eval is needed to turn the text version of the rewritten function back into code - and since the eval is wrapped around the entire content of the function it has the same scope as the function and nothing defined within it needs to exist afterwards.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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