Solved

Problem w/ onunload event

Posted on 2001-07-03
35
481 Views
Last Modified: 2012-06-27
I open app window. I have it open up a 1 frame frameset, so that I can go to a logout page when the window is closed by the user. Heres the code for the frame. It works in IE, but in Netscape the onunload event never seems to fire:

<script language="javscript">
     function LogOut() {
          self.opener.location.replace("logout.cfm");
     }
</script>
     
<frameset title="Rapid MMS" onunload="LogOut()" rows="100%,*">
     <frame src="welcome.cfm" name="main" frameborder="0" scrolling="Auto" framespacing="0" title="Rapid MMS" border="0">
</frameset>

Any help would be much appreciated. I'm sure I am doing something stupid.

:) dapperry
0
Comment
Question by:dapperry
  • 18
  • 9
  • 7
  • +1
35 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249031
Should work...could it be because of:

<script language="javscript">

which should be:

<script language="javascript">


????

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249037
Or could it be because the reference to the opener gets lost........

:o\

Ant
0
 
LVL 3

Author Comment

by:dapperry
ID: 6249197
Fixed the <script language="javascript"> and it still doesn't work. What else could be wrong?

:) dapperry
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249210
What version of Netscape are you using?

Try using this to try and narrow down where the issue is:

onunload="alert('test!');"


Ant
0
 
LVL 3

Author Comment

by:dapperry
ID: 6249232
I'm using Communicator 4.7

I tried that, but no alert seems to come up.

:) dapperry
0
 
LVL 3

Author Comment

by:dapperry
ID: 6249248
Do you think its something in the way I set up the frameset itself?
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249262
No - but it wouldn't surprise me if the problem is the fact that you are using 4.7 - that was a buggy version of Netscape - could you try using a different version (older or newer)?

Ant
0
 
LVL 3

Author Comment

by:dapperry
ID: 6249725
I upgraded to 4.77 and still got the same problem. Now I have Netscape 6, but it sucks becuase its slower than molasses, but the alert does come up. Anyhow, I can't very well create an app that won't work a great many browsers. Funny thing though if I have onunload="self.opener.location.replace('logout.cfm')" it still doesn't work. However, in this instance if I look at the Javascript console I get the following error message:

JavaScript Error: illegal URL method 'logout.cfm'

Does this tell anybody anything?

:) dapperry

PS - I'm upping the pts so I can get this resolved quickly
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249765
What about putting the onunload in the frame instead of the frameset?

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249793
My last comment is unlikely to help to be quite honest - but worth a try!

Ant
0
 
LVL 3

Author Comment

by:dapperry
ID: 6249801
Just tried putting it in the frame, but it still didn't fire the event.

:) dapperry
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249807
The javascript error you are talking about - does it appear in Netscape 4 or 6?

If 4, then do you get an error when you try the alert()?

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249814
You mentioned earlier about the fact you only have one frame, perhaps try:

<frameset title="Rapid MMS" onunload="LogOut()" rows="100%,*">
<frame src="welcome.cfm" name="main" frameborder="0" scrolling="Auto" framespacing="0" title="Rapid MMS" border="0">
<frame src="">
</frameset>
0
 
LVL 3

Author Comment

by:dapperry
ID: 6249863
That didn't work, but this does:

<script language="javascript">
     function LogOff() {
          self.opener.alert("hi");
     }
</script>

<frameset title="Rapid MMS" onunload="LogOut()" rows="100%,*">
                     <frame src="welcome.cfm" name="main" frameborder="0" scrolling="Auto" framespacing="0" title="Rapid
                     MMS" border="0">
                     </frameset>

So is there something wrong with:
     self.opener.location.replace("logout.cfm");

:) dapperry
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6249881
What about just trying:

self.opener.location = 'logout.cfm'


Ant
0
 
LVL 3

Author Comment

by:dapperry
ID: 6249915
Just tried that, and it didn't work either. Still got the:

illegal URL method 'logout.cfm'.

This the case in any Netscape 4.7x. I get no errors in NS 6

Weird, but alert(self.opener.location.href); actually gives the correct URL for the parent window that opened the App window, but then when you try to set the same property it gives you the error I just mentioned above. Maybe its losing the opener reference. Is there anyway to get some sort of index for the opener window, and then try to reference the opener window that way?

:) dapperry
0
 
LVL 3

Author Comment

by:dapperry
ID: 6250058
Maybe upping the pts some more will get some more chefs into the kitchen (No offense Ant, but I need an answer)

:) dapperry
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:rpolocz
ID: 6250544
so setting window.opener.location.href='logout.cfm'; also gives the error?  If so, try lowering the browser security permissions.  And logout.cfm is the correct local path, not /logout.cfm?  And logout.cfm complies correctly?  Did it work in NS 6?
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6250623
Okay - I've been playing around with this a little bit and have come across something strange....

Instead of doing the redirect in the child window, I thought we could do it in the parent window. Try the following code and see the odd effect you get in Netscape 9again it works perfectly in IE.

test.html
------------------------------------------------

<html>
<head>
<script language="javascript">
<!--

var newwin = window.open("test2.html", "", "width=200,height=200");

function test(val){
  alert(val);
  window.location = val;
}

//-->
</script>
</head>
<body>
</body>
</html>


test2.html
------------------------------------------------

<html>
<head>
<script language="javascript">

    function LogOut() {
        self.opener.test("logout.cfm");
    }
</script>
</head>
<frameset title="Rapid MMS" onunload="LogOut()" rows="100%,*">
    <frame src="welcome.cfm" name="main" frameborder="0" scrolling="Auto" framespacing="0" title="Rapid MMS" border="0">
</frameset>
</html>


Now if anything it proves that in Netscape you can still reference the window.opener okay - there is just some real problem with redirecting.

If you call the test() function from within the parent window, for example, add this code to the parent window:

<a href="javascript: test('logout.cfm');">click me</a>

the function works fine!!

:oP

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6250641
Got it!!!!!!!!

For some reason (I've seen this issue before) Netscape needs to have a slight delay.

All you need to do is change one line of your original code.

Change:

self.opener.location.replace("logout.cfm");

to:

self.opener.setTimeout("window.location.replace('logout.cfm');", 10);


And it works in Netscape!!

:o)

Ant
0
 
LVL 15

Accepted Solution

by:
a.marsh earned 300 total points
ID: 6250646
I have tried and tested the code and it works in Netscape (aswell as IE).

Just to clarify....

Change:

self.opener.location.replace("logout.cfm");

to:

self.opener.setTimeout("window.location.replace('logout.cfm');", 10);


Wrapping this question up.

:o)

Ant
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6254907
Really???

I would have thought that the unload cleared too much...
Does it work if the user click the [x] too????

I would have thought this woudl be safer:

in opener:
<script>
window.name="mainwin"
</script>

in popup

<frameset onUnload="window.open('logout.cfm','mainwin','replace')">

Michel

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6254909
even
<frameset onUnload="window.open('http://www.server.com/logout.cfm','mainwin','replace')">

to be on the safe side in case Netscape forgets where he is

Michel
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6254925
Yes, really! :oP

Ant
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6254935
Also when the window is closed??? It never used to work

Michel
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6254952
We don't need to open a new window though Michel - we are simply changing the location of the parent window.

The onUnload event was firing in Netscape - we were just getting an error when trying to change the location of the parent window - and the way that I solved it was to set a timeout on the parent window to do the redirection.

I tried it on Netscape 4.7 on Windows and it worked.

:o)

Ant
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6255060
That is not what I mean - IF the user closes the window I think you STILL want to logout, no?
And I wonder if it still finally in Netscape to have an onload that is triggered by a closing window call a script that uses window attributes like a document.form or an opener

Michel
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6255084
The situation as I see it is that the user logs in and (on successful login) a popup window is created (which contains a frameset so as to "hide" the URLs of the pages visited).

Upon the user closing that popup window, the system forces a logout by redirecting the parent window (that opened the child window) to the logout.cfm page.

The issue was that Netscape didn't seem to want to redirect the parent window - hence I discovered the way to get it to do that:

self.opener.setTimeout("window.location.replace('logout.cfm');", 10);


:o)

Ant
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6255179
yeees, and my question was, does your timeout actually work when the user CLOSE the window and not just navigate away in that window - if so a very old netscape problem has been solved - e.g netscape forgets everything including scripts and openers when the x in the corner is used to close a window

Michel
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6255194
And as I've already said - yes it does work when you close the window - at least in version 4.7 on Windows which I have tested.

:o)

Ant
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6255265
Did you try to look at the javascript console?

I get "JavaScript Error: out of memory "

but it does work in ns 4.7 on NT

Michel
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6255275
I've tried it with Netscape 4.7 on Windows ME - everything worked fine....

Ant
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6255295
The out of memory error does however support my notion that NS is very unhappy doing anything onUnload
Using the opener to do it's own location change is clever though

Michel
0
 
LVL 3

Author Comment

by:dapperry
ID: 6259585
Yes, that works completely. Cool!! Yes, the pop up window is fired after the person logs in. I actually make the window without menus, and also use a right mouse disable js function (that I believe Michel gave me a while back), so that the user has to use my navigation buttons, and the 'Back' button, etc. doesn't come into play. When I close the pop-up window (and destroy the 1 frame frameset) the logout page is loaded in the parent window, so I can do some logging, etc. Thanks again!

:) dapperry
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6259598
Glad to have helped.

Thanks for the A!

:o)

Ant
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now