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 12-02-2012, 12:09 AM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
eval output

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

Code:
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

Code:
window.close(page25);

and also 

window.close(25);
and it did not close the window, so thats why im curious about this

Last edited by durangod; 12-02-2012 at 04:49 AM..
durangod is offline   Reply With Quote
Old 12-02-2012, 12:34 AM   PM User | #2
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
You should avoid eval():

Code:
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:
Code:
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..]
__________________
"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
Users who have thanked AndrewGSW for this post:
durangod (12-02-2012)
Old 12-02-2012, 01:03 AM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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/showthre...l%28%22page%22
durangod is offline   Reply With Quote
Old 12-02-2012, 01:24 AM   PM User | #4
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
i scrapped the eval and just did it normally

Code:
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


Code:
onclick="chatwindow.close();

the other one in the js is just chatwindow.close();
neither of those close the window, and not sure why
durangod is offline   Reply With Quote
Old 12-02-2012, 01:35 AM   PM User | #5
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
Is chatwindow global? And what does the console say?
__________________
"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 12-02-2012, 01:46 AM   PM User | #6
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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.

Code:
<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,top=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

Code:
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

Code:
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,top=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...

Last edited by durangod; 12-02-2012 at 02:48 AM..
durangod is offline   Reply With Quote
Old 12-02-2012, 03:14 AM   PM User | #7
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Ok i been doing some reading. Accounding to this rule

Quote:
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

Code:
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.

Last edited by durangod; 12-02-2012 at 04:17 AM..
durangod is offline   Reply With Quote
Old 12-02-2012, 04:48 AM   PM User | #8
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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..

Thanks all.
durangod is offline   Reply With Quote
Old 12-02-2012, 05:05 AM   PM User | #9
felgall
Master Coder

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

Code:
(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

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

// your code to use jQuery goes here

})(jQuery);
__________________
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
Users who have thanked felgall for this post:
durangod (12-02-2012)
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 06:47 AM.


Advertisement
Log in to turn off these ads.