PDA

View Full Version : how to capture the window close event


t_ravichandu
04-14-2004, 11:28 AM
hi folks,
I am a beginner to the web designing field,
I wanna know how can I know that a window is closed so that i can open another window.
chandu

agent002
04-14-2004, 11:31 AM
who of the mods is on a bad mood today?

t_ravichandu
04-14-2004, 11:35 AM
thanx for the reply
i really don't have a clue of what u r talking....
what is window->close....

Goldilocks
04-14-2004, 11:40 AM
None of was were, chandu posted this thread right here in the Spam Box, though I'm not sure why! ;)

I'll move it to a more appropriate place.

agent002
04-14-2004, 11:43 AM
ok. Well, I have some questions to you first. Whatever you're looking for, it most probably requires JavaScript. How much do you know about HTML, are you ready to start with JavaScript yet? You should have some experience of the HTML basics first. Also, could you please describe better what you really want, and what you want it for? You can use the window.onunload event handler to execute stuff when the window is closed or another page is loaded to the window.

t_ravichandu
04-14-2004, 11:46 AM
well...i'mmm..new to this site
and having a lil trouble to figure out things here:confused:

Goldilocks
04-14-2004, 11:49 AM
Each of the forums has a description under it describing what it is for. The Spam Box is where posts get moved if they are breaking forum rules, i.e. advertising, cross-posting, etc.

Don't worry though, if you post in the wrong place it will soon get moved by one of the moderators.

agent002
04-14-2004, 11:51 AM
Originally posted by t_ravichandu
well...i'mmm..new to this site
and having a lil trouble to figure out things here:confused:
no problem, such small accidents happen very often with newbies, but we don't care :) But as a little tip, I'd suggest you take a moment to read through the description of every forum category in the forum main index, and also read the rules in the thread marked with "IMPORTANT" in the current forum category, before creating a thread in it. Also using the search before posting a question is a good idea, as somebody might have asked your question before and the answer might be available already. If you still don't find the answer you want, just post a new thread. Also reading the FAQ (see links in the header of this page) once is a good thing to do.

Now, would you mind answering the questions I asked in my previous post?

t_ravichandu
04-14-2004, 11:53 AM
well i think i know some thing abt html

lemme describe my need

i'v a webapplication which interacts a lot with backend uisng java beans and i use jsp for server side stuff...

my application has a requirement that when a user logins no other can login with the same user name. so i'm saving the logged in user names in backend and when he logsout i remove it from backend.

now my problem is when one closes the window without clicking on logout but clicking on the 'X' of browser window i'v to open some other window and have to remove that user from the backend.
so that's my need
pls help
i'm in serious urgency
chandu

agent002
04-14-2004, 12:03 PM
You've got yourself into a little tricky position, but I'd suggest you make a login session timeout for the script as the first thing. If the logged in user doesn't do anything in maybe 10 or 15 minutes, his logged in status expires. Then, you can try to add on a JavaScript. You could use a window.onunload to open a popup when exiting, and the popup page logs one off. But that causes a new complication; the window.onunload event triggers just when you go to a new location, i.e. click a link or submit a form... so you need special links to disable the onunload event. Try this code I wrote for you; put this JavaScript between the <head> and </head> tags:
<script type="text/javascript">
var doExitPopup = true;
window.onunload = function(){
if(doExitPopup){
window.open('logMeOut.jsp', '_blank', 'width=100,height=80');
}
}

function interalLink(){
doExitPopup = false;
}
</script>
Then you must use this code for every link, to not trigger the onunload event when clicked:
<a href="targetUrl.jsp" onclick="internalLink();">Click here</a>
In the JavaScript code above, change logMeOut.jsp to the name of the file that logs you out; on the page it prints, use a simple JavaScript to close the popup window it was opened to:
<script type="text/javascript">
window.close();
</script>
If the user's browser has JavaScript disabled, or popup windows disabled, this popup window won't open, but then the login session will expire in the time you set (as I adviced you earlier in this post). I hope that helps. :)

t_ravichandu
04-14-2004, 12:57 PM
thanx it works
but with a problem
you said to keep the internallink() function in every link but it is not working(may b i don't know how to keep it) since my page contains frames. Every link is working fine bcaz the target is the main frame, but for logout link the tartget is "_top" so it is giving double effect of logging out twice leading to an error msg.
pls tell me how to call the internallink() function from another frame.
chandu

agent002
04-14-2004, 01:03 PM
Please remember it's internalLink(), not internallink... JavaScript is case-sensitive. But anyway, you should be able to call the other frame's internalLink() function by using parent.framename.internalLink(). So if the other frame's name is headerframe, use:
onclick="parent.headerframe.internalLink(); internalLink();"
in the link. :)

t_ravichandu
04-14-2004, 01:29 PM
this works but sort of don't work i mean it is working when i change '_blank' parameter in window.open() to '_top' and it is working as expected when i click on logout link and it is not even showing the opened page when closed by clicking the close button(X) of window. But the logic in logMeOut.jsp is working fine.
and i also figured out that the session will remain intact even in the newly opened window. am i right?
chandu

agent002
04-14-2004, 01:33 PM
well, logMeOut.jsp should open in a new popup window, it can't open in the original window as that is closed! That's why I suggested you to put the window.close() javascript on logMeOut.jsp to close it as soon as the JSP has run, so the popup will actually just flash for a second maybe logging you out.

t_ravichandu
04-14-2004, 01:40 PM
any way i'v a doubt regarding this line of code

onclick="parent.headerframe.internalLink(); internalLink();"

y the need for calling the functin two times.
chandu

agent002
04-14-2004, 01:45 PM
let's say you have a frameset in index.html, the frameset shows a.html and b.html. Both a.html and b.html contain the internalLink() functions. If you have a link in b.html that is targeted to _parent or _top, i.e. replaces the frameset with the href, the onunload event is triggered both in a.html and b.html, as both are unloaded.

t_ravichandu
04-14-2004, 02:04 PM
thanx very much for ur patient replys
and they r all right to the point :bullseye:
i'v 2 say u came like an :angel: for me
but i still hav a doubt
y's ur name agent002 and not agent007
chandu

agent002
04-14-2004, 02:10 PM
haha, thank you. Well, I chose this username probably some four-five years ago when I signed up at a community for the first time of my life. I feel it's stupid to directly take a famous name (in this case agent007, mr. Bond), but much better to modify it in a clever way, like agent002. So I'm not James Bond, I'm some of his less famous partners :D

Nowadays, I don't use this name anymore when I sign up at places; I rather use Justin Case.

t_ravichandu
04-14-2004, 02:13 PM
so justin
thanx once again 4 ur patient support
i'l b back with some more doubts
cheers
chandu