...

View Full Version : Resolved eval output



durangod
12-02-2012, 12:09 AM
Hi i am trying to close a window that is assigned by an eval, so i need to have the name of the window, but i dont know how to output the result to the screen or page so i can see what it looks like


eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0, blah blah lol

so if in js id=25

then would the name of the window in this example be page25

im not sure why the starting name = has page and the internal name just has the id (after URL) they should be assigned the same i think.

i did a test and made id=25 then i opened the window and then ran a button test to



window.close(page25);

and also

window.close(25);




and it did not close the window, so thats why im curious about this

AndrewGSW
12-02-2012, 12:34 AM
You should avoid eval():


window["page" + id] = window.open(URL, '" + id + "', 'toolbar=0, blah blah lol
creates a global variable named 'page25' which you can later use to close the window:

page25.close(); // [close() has no arguments] or
window["page" + id].close();
The name is pretty useless, other than as the target for links, as it cannot be used to refer to the window.

[Strange.. a very similar question arose recently..]

durangod
12-02-2012, 01:03 AM
Yep your right sorry, i did do a search but this one bit me for sure. My bad

Here is the link if anyone wants it, sorry about that

http://www.codingforums.com/showthread.php?t=283239&highlight=eval%28%22page%22

durangod
12-02-2012, 01:24 AM
i scrapped the eval and just did it normally



chatwindow = window.open(URL, 'basicchatw', "location and so on ........



then on another page i have two tests

one with a button and one in js hard coded in an if statement



onclick="chatwindow.close();

the other one in the js is just chatwindow.close();

neither of those close the window, and not sure why

AndrewGSW
12-02-2012, 01:35 AM
Is chatwindow global? And what does the console say?

durangod
12-02-2012, 01:46 AM
im not good with js i dont even know how to use the console to be honest. i know in php i shoot stuff to the page all the
time to check val, but honestly i never learned how to shoot val to a page so i can see it with js by way of any console. im so green.

Thats why this is hard cause im flying blind here, stabbing in the dark..

I can tell you that this is set in the main member header php file. i changed the names to do some trouble shooting and in my php file i have both set to close.




<script type="text/javascript">

function popUp(URL)
{

chatwbasic = window.open(URL,'basicchatw',"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=615,height=444,left=395,to p=200");

}//close function

</script>


and then as a test in one of my php files i have this

<script>
basicchatw.close();
chatwbasic.close();
</script>



and the window is still open.


UPDATE i just read about globals and i hope i did this right

outside of the function but within the js i did this cause i want to see if it works, if it does then i will just pick one name

so this should set both as global i think and still does not close the window




var chatwbasic;
var basicchatw;




Another update here: i did read about console using ff console and it told me i need to add this to my form so i did. still nothing



accept-charset="UTF-8"

it also told me that

[19:18:03.407] TypeError: basicchatw is undefined @ http://www.xxxxxx/cxxxcomplete.php:914
but i expected that because chatwbasic is the actual name im sure.

and now it is telling me that

[19:24:04.120] ReferenceError: basicchatw is not defined @ http://www.xxxxx.com/cxxxcomplete.php:370

i guess putting var and the name like this doesnt define it or do anything

<script type="text/javascript">

var chatwbasic;
var basicchatw;

function popUp(URL)
{

chatwbasic = window.open(URL,'basicchatw',"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=615,height=444,left=395,to p=200");

}//close function

</script>

and im back to square one lol ...

still not defined after doing

var chatwbasic = "";

and i even tried adding var to the window open command

var chatwbasic = window.open ...........

how do i make chatwbasic global, it must be a secret that only the top top top coders know about lol...

durangod
12-02-2012, 03:14 AM
Ok i been doing some reading. Accounding to this rule



Any variable that is initialized inside a function using the var keyword will have a local scope. If a variable is initialized inside a function without var, it will have a global scope. A local variable can have the same name as a global variable.




then saying


windowname = window.open(.................................

windowname should be script wide (global) var?


so on any page of my script, if i say windowname.close() if it is open it should close it. But it does not.

I wish there was a way to just get a list of all the open windows and their names

lol i also tried standing on one leg, jumping up and down while tapping my mouse 3x and wispering the theme song to gilligans island LMAO but that too didnt work...

update: ok now i even moved the window call function from the header file to the local file and still wont close the window.

durangod
12-02-2012, 04:48 AM
solved, i did two things but i prop only had to do one.

i had onclick="windowname.close(); /> <!-- i missed the closing quote omg -->

and i also changed the function name and moved it into the local file.

now when i click close button it closes.

shame on me for missing that quote.. :mad:

Thanks all.

felgall
12-02-2012, 05:05 AM
Any variable used in modern JavaScript MUST be declared using var in order for the script to run. It is only when you leave out the "use strict"; declaration with antiquated JavaScript that you can use variables without declaring them.

You should avoid declaring global variables as they WILL cause script clashes. The most you would need is one global variable so as to be able to reference a framework library from within your script.

A good starting point for all JavaScript is the following:


(function() {
"use strict";
var i; // comma separated list of your variables

// your code goes here

})();

With that wrapped around all your code you can be certain that it will not interfere with other scripts. The variables you declare in the var statement will be "global" as far as your script is concerned but will be hidden from any other scripts in the page.

If you are using a framework such as jQuery then you'd have the one global variable and would change your starting point as follows to allow that global variable to be referenced using its short name from within your script even if the short name is disabled to avoid conflicts with a second framework


(function($) {
"use strict";
var i; // comma separated list of your variables

// your code to use jQuery goes here

})(jQuery);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum