Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-29-2012, 05:12 PM   PM User | #1
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,602
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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().
__________________
Don’t click this link!

Last edited by VIPStephan; 11-29-2012 at 05:16 PM..
VIPStephan is offline   Reply With Quote
Old 11-29-2012, 05:34 PM   PM User | #2
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
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 06:38 PM..
007julien is offline   Reply With Quote
Old 11-29-2012, 05:59 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"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

Last edited by AndrewGSW; 11-29-2012 at 06:05 PM..
AndrewGSW is offline   Reply With Quote
Old 11-29-2012, 06:28 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 11-29-2012 at 06:32 PM..
felgall is offline   Reply With Quote
Old 11-29-2012, 08:35 PM   PM User | #5
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,602
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 11-29-2012, 08:46 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 11-29-2012, 08:54 PM   PM User | #7
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,602
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 11-29-2012, 09:39 PM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 11-29-2012, 09:54 PM   PM User | #9
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"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

Last edited by AndrewGSW; 11-29-2012 at 10:50 PM.. Reason: Added code
AndrewGSW is offline   Reply With Quote
Old 11-29-2012, 10:24 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
..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
AndrewGSW is offline   Reply With Quote
Old 11-29-2012, 11:09 PM   PM User | #11
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 11-30-2012, 08:12 AM   PM User | #12
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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:

Quote:
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/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 05:32 PM.


Advertisement
Log in to turn off these ads.