Bloomin' javas...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] Bloomin' javascript!

28 Posts
10 Users
0 Reactions
125 Views
Posts: 91000
Free Member
Topic starter
 

So - due to the weird design of this application, the main window opens another in which one does one's work. The 'logout' button simply closes the work window and redirects the original one. However if you close the original window manually the work window keeps working, then when you click the aforementioned logout button you get a javascript error of course. The logout button tries to call a javascript function in the parent window.

So if the main window has been closed all I want to do is close the work window when the user clicks logout. However that does not seem to be possible. Looks like only the parent window can close the child window - it can't close itself. Is that right?


 
Posted : 21/07/2011 9:00 am
Posts: 5686
Full Member
 

? Do you want the child to call Parent.window.close() or something similar?

Javascript:alert("rusty");

🙂


 
Posted : 21/07/2011 9:27 am
Posts: 91000
Free Member
Topic starter
 

No. Currently it calls a function in the parent that closes the child. However if the parent's been closed, then that won't work.

I call window.close() in the child window and it does nothing. No error either. Same in IE8 and Chrome. So I am thinking it's meant to be that way - a child can't close itself.


 
Posted : 21/07/2011 9:29 am
Posts: 3544
Free Member
 

window.close() should work in the child regardless, just tried it on one of my pages. Are you trying to call anything before that that is possibly blowing out the javascript (like trying to hit the parent) before the call?


 
Posted : 21/07/2011 11:58 am
Posts: 91000
Free Member
Topic starter
 

Er... let's see.

function logofftr() {
if (confirm('Do you want to logout?'))
{
if (wlWindow && !wlWindow.closed) {
wlWindow.close();
}
if (parent.opener && !parent.opener.closed) {
parent.opener.logofftr();
} else {
window.close();
}
return false;
}
else
{
return false;
}
}

wlWindow is not significant - that's another window opened by this that I don't care about. opener.logofftr() just calls a function in the parent that closes the child window...


 
Posted : 21/07/2011 12:01 pm
Posts: 0
Free Member
 

I no nothing about programming but should the last five squigly brackes be closed, closed, open, closed, closed? they seem to be open then closed round certain sections of code above


 
Posted : 21/07/2011 12:06 pm
Posts: 91000
Free Member
Topic starter
 

No. The brackets are ok 🙂


 
Posted : 21/07/2011 12:08 pm
Posts: 0
Free Member
 

as i said i know nothing, good luck in gertting it sorted.


 
Posted : 21/07/2011 12:10 pm
Posts: 12072
Full Member
 

What happens if you stick "window.close();" as the first line of the function?


 
Posted : 21/07/2011 12:31 pm
Posts: 5686
Full Member
 

So the parent is closed when this is called and you just want the child to close? Is there an error being caught on checking if (parent.opener && !parent.opener.closed) as the parent is null, which then prevents the window.close in the else from executing?

I'm not sure I can help, but I'm vaguely interested 🙂


 
Posted : 21/07/2011 12:33 pm
Posts: 1672
Full Member
 

Am sure you can close a child window - been a while since I last tried though. Does it work if you take out everything apart from window.close()?

IE/Chrome Developer tools and Firebug all have javascript debuggers, so you should have fixed this by now! 😆


 
Posted : 21/07/2011 12:37 pm
Posts: 91000
Free Member
Topic starter
 

Had the debugger out. It just executes the line and nothing happens.


 
Posted : 21/07/2011 12:43 pm
Posts: 1672
Full Member
 

Which line, the last window.close()?

Is wlWindow a variable that is declared in scope etc (not that javascript always cares about this).


 
Posted : 21/07/2011 1:23 pm
Posts: 91000
Free Member
Topic starter
 

It's not the wlWindow I care about - that's another child window.

It's the window in which this very JS is running.

It didn't work when I removed the entire thing apart from window.close either.

Security setting?


 
Posted : 21/07/2011 1:28 pm
Posts: 0
Free Member
 

[s]Quite possibly, yes.[/s]

Actually I don't know but this works for me

function CloseWindow() {
window.close();
}

and then IE pops up a message "The webpage you are viewing is trying to close the window. Do you want to close this window?". That would suggest that IE has got the final say. And there are a bunch of IE settings that deal with Javascript


 
Posted : 21/07/2011 1:36 pm
Posts: 349
Free Member
 

Ask on StackOverflow, you'll probably get an answer for this within minutes 🙂


 
Posted : 21/07/2011 1:37 pm
Posts: 2
Free Member
 

Does the child window have another name from when it was opened?

myWindow=window.open('','','width=200,height=100');

myWindow.close ();

Or try using "self.close()"

My knowledge is probably as good as rusty Mac but I am learning slowly


 
Posted : 21/07/2011 1:49 pm
Posts: 0
Free Member
 

Do you really need all those spurious curly braces for the one-line statements? without any indentation they just serve to confuse and they also take up vertical space pointlessly.

The only bug I have ever seen that is related to curly braces and if statements was actually the other way round than you would normally think, as in someone had:

if (a)
{
<indent>blah1
}
else
{
<indent>blah2
}
<indent>blah3

if they only used braces for when it was a multi-line condition it would be obvious what was wrong.


 
Posted : 21/07/2011 2:13 pm
Posts: 1672
Full Member
 

TG, why start a coding standards argument?! If the last line wasn't indented your example code would be very readable. It isn't Molgrips problem anyway.

MG, if I don't at least do var wlWindow; in the child window I get this message:

'wlWindow' is undefined

And everything stops. Otherwise though, it works for me.


 
Posted : 21/07/2011 2:27 pm
Posts: 0
Free Member
 

I no nothing about programming but should the last five squigly brackes be closed, closed, open, closed, closed? they seem to be open then closed round certain sections of code above

because someone was confused - proving my point.


 
Posted : 21/07/2011 2:34 pm
Posts: 1672
Full Member
 

The guy didn't know anything about programming though! Also he'd be able to read it easier if the indenting was kept when Molgrips pasted the code.

[url= http://stackoverflow.com/questions/254864/would-it-be-bad-form-to-put-braces-on-the-same-line-as-the-statement-for-single-l ]Dorky discussion about closing braces etc etc etc (etc)[/url]

I've got to get back to my real job of putting curly braces in the wrong place 😛


 
Posted : 21/07/2011 2:48 pm
Posts: 0
Free Member
 

there is no argument about where to place braces - Java, Javascript, C#, C++ are all C-based languages so the answer is to use the same style as the inventors of C - easy.

code layout is a legitimate point of discussion as it has been shown that if an experienced programmer is shown code in a format that he is not used to then he is not much better than a junior programmer at spotting issues with it.

Hence the correct format is the one most people use, unless it is anything suggested by microsoft.

normally that would be in the same style as the inventors of the language, so K&R for C, Stroustrup for C++, Gosling for Java, etc.


 
Posted : 21/07/2011 2:55 pm
Posts: 349
Free Member
 

The answer is to put the whole thing into a load of nested ternary operators thus not needing any curly braces.


 
Posted : 21/07/2011 2:57 pm
Posts: 0
Free Member
 

The answer is to put the whole thing into a load of nested ternary operators thus not needing any curly braces.

and that would only be the answer if that was the idiomatic way of programming in that language.

That is why Perl is so poor, perl programmers delight in that tmtowtdi rubbish.


 
Posted : 21/07/2011 3:02 pm
Posts: 349
Free Member
 

I was only joking! I write a lot of python, I indent everything now.


 
Posted : 21/07/2011 3:06 pm
Posts: 1672
Full Member
 

function logofftr() {
if (confirm('Do you want to logout?'))
{
if (wlWindow && !wlWindow.closed)
wlWindow.close();
if (parent.opener && !parent.opener.closed)
parent.opener.logofftr();
else
window.close();
return false;
}
else
return false;

Fixed for TG and Rusty Mac! Now can someone please help Molgrips?

p.s. Spot the change I inadvertently introduced.


 
Posted : 21/07/2011 3:22 pm
Posts: 0
Free Member
 

not sure I can see it apart from losing the last brace.

why does the function always return false - just because you have to return something with a function? (not a javascript programmer)

otherwise I would have thought:

[code]
function logofftr()
{
....if (confirm('Do you want to logout?'))
....{
........if (wlWindow && !wlWindow.closed)
............wlWindow.close();
........if (parent.opener && !parent.opener.closed)
............parent.opener.logofftr();
........else
............window.close();
........return true;
....}
....return false;
}
[/code]

(where . = space 'cos I don't know how to BBCode it)

I assume that the parent window doesn't have the same definition of logofftr and is asking for confirmation as well?


 
Posted : 21/07/2011 3:40 pm
Posts: 91000
Free Member
Topic starter
 

MG, if I don't at least do var wlWindow; in the child window I get this message:

'wlWindow' is undefined

Yeah this is only a small part of a script, wlWindow is defined elsewhere.

If you guys get a dialog box then I'm thinking it's security settings.


 
Posted : 21/07/2011 4:40 pm
Posts: 0
Free Member
 

Wo wo wo wo wo what am I being dragged into now, I don't do programming. Just. Remembered from uni that formatting was important to it running smoothly and additional symbols can close other parts of the program un intentionally and some times these things are over looked by someone looking for a more complex problem. Hopefully mol will have the problem sorted now any way


 
Posted : 21/07/2011 4:59 pm

6 DAYS LEFT
We are currently at 95% of our target!