Solved

Need to run code in Unload Event

Posted on 2004-08-25
29
236 Views
Last Modified: 2008-02-01
Hi, simple question. I have a Flash Forms application and i need to run some code before the user closes the application. I know the forms class extends the UIObject class so I have tried to use the following code only to no avail! The only form i have is form1. Can someone please help me??

listenerObject = new Object();

listenerObject.unload = function(eventObject){
//this code never gets executed on the unload event!!!!!
}

this.addEventListener("unload", listenerObject)

0
Comment
Question by:Maxim10553
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 11
  • 5
29 Comments
 
LVL 10

Expert Comment

by:muso120999
ID: 11893006
> i need to run some code before the user closes the application

By this, do you mean when the user closes the browser window, or Flash player?

If so - I think if it's possible - it will be pretty difficult, and will probably have to involve a JavaScript event (onClose?) to pass a value to Flash when this is triggered.
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11893223
when the users closes the application i want the code to be kicked off (to clean up some files) - so it would be the browser window - im looking for an actionscript answer. thanks for your quick response. I dont understand why the above code does not work!
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11893918
It doesn't work, because the unload event is never going to be triggered.  It is up to the browser as to how it handles the closure, and the Flash object isn't  "informed" of the event, which is why you would need JavaScript to intercept that.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Maxim10553
ID: 11893987
can you provide an example
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11894035
I'll see if I can come up with something better if you liike, but here's something for now...
http://www.kirupaforum.com/forums/archive/index.php/t-17543.html
0
 
LVL 3

Expert Comment

by:chrisevans123
ID: 11923788
Hi Maxim,

The solution above - whilst entirly correct is not very complete.  I would imagine from what you are saying, you need to actually communicate with the swf file before the window is closed.  To do this, you need to follow what has been said so far i.e. create a javascript event that executes when the browser has been closed.  At this point, we need a reference to the activex swf file - then we can set a variable i.e.

N.B The name and id attribute of the object tag have to be set - in this case they would be set to login.

<SCRIPT LANGUAGE="JavaScript">
<!--
function closedWin(){
//create a variable to hold a reference to the activex object
//set the variable based on the differing browser object models
var swf = null;
if (navigator.appName == "Microsoft Internet Explorer") {
      swf = document.getElementById("login");
} else {
      swf = window.document.login;
}
//once we have a reference to the swf file we can set a variable
swf.SetVariable("closing", "true");
}
-->
</SCRIPT>

In the actual swf file itself, create a variable in the root timeline called closing and set it to a string value "false".  Next create a new movieclip and add the following code to the first frame:

onEnterFrame = function() {
if (_root.closing == true) {
//add code her to execute when the user closes the window
}
}

hope this helps,

Chris

P.S For more info on setting variable etc via javascript see the link below:

http://www.macromedia.com/support/flash/ts/documents/tn4160.html

0
 
LVL 3

Expert Comment

by:chrisevans123
ID: 11923796
Sorry - the onEnetrFrame method should look like this:

onEnterFrame = function() {
if (_root.closing == "true") {
//add code her to execute when the user closes the window
}
}

notice the quotes.
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11925053
Thanks for helping out with an example there chrisevans123.

Just an aside, is that you can use watch rather than onEnterFrame, which, I think, is less dependant on where it is in the movie.

ie
_root.watch("closing",callback_function)
0
 
LVL 3

Expert Comment

by:chrisevans123
ID: 11925142
You can indeed - that's an old habbit of mine from flash 5 :)
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11926565
Having not actually tried it myself: does this solution actually work!?
0
 
LVL 3

Expert Comment

by:chrisevans123
ID: 11926587
Yes it does - if you want to see it in practice, goto http://www.westcoastproperties.uk.com/home/default.aspx - click a link from the main menu and watch the submenu on the top left - that uses the exact script we've been discussing.

Chris
0
 
LVL 3

Expert Comment

by:chrisevans123
ID: 11926604
Soory - the events are slightly different - the event is fired when the iframe's body has been loaded instead of closing the browser - but the theory is exactly the same.
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11926616
Yeah, I was just wondering that Chris; thanks for the example though.  I have also been wondering whether JavaScript would need to hold the browser - preventing it from closing - until Flash had called-back saying that the data had been flushed.

muso
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11930837
Well the problem with using javascript is that in the end I will be publishing an executable rather than an html file that embeds the swf
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11930857
sorry, i guess muso asked that question in the beginning - (By this, do you mean when the user closes the browser window, or Flash player?) and i said browser window - which was incorrect. Hope I am still able to catch the event
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11930859
> so it would be the browser window - im looking for an actionscript answer

I guess your earlier comment threw me then.

Well, I know if you are using Director or Authorware you can perform a very similar function using getURL, and it would be more reliable to control it than with a browser anyway

muso
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11931002
??
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11931014
I have created the watch which seems to be working fine when i test it; now i need to catch the close event and change the variable somehow.
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11931118
I can't help you without knowing what you are deleloping the exe in.
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11931144
sorry I guess I am not understanding. I am using Flash to create my application and also publish it into an exe
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11931228
I could be wrong, but I don't think you can do it then :-(

Unless you import it into something like Director, or Authorware - you can intercept the close event using something like BuddyAPI, and pass back instructions to Flash to close gracefully.  I think this is the only way to give you that flexibility.
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11945517
Would SWF Studio Work?
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11945536
I have access to getURL in SWF studio
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11945582
I guess it's possible then.  You would need to create an event that triggers in SWF Studio to send back the onClose notification though.

I'm afraid I haven't used SWF Studio, so I don't think I could help you much with that side if it myself.
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11945600
okay i know you spoke briefly about GetURL in a comment above - can you discuss that option a little more?
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11946043
I guess I was slightly misleading with that comment: I have done a little development with Authorware and Director with Flash and I know that you can use functions like setSpriteProperty to set variables in Flash, or to call specific points of the movieclip to play.

getURL is just your method of communicating from Flash back to certain applications - ie. HTML, Director, Authorware.  It could be used, for example, to flag when your clean-up routines have completed; it's no use without first identifying when the window is about to close though.
0
 
LVL 10

Accepted Solution

by:
muso120999 earned 200 total points
ID: 11946137
Do you feel that we are making any progress here Maxim10553?
0
 
LVL 3

Author Comment

by:Maxim10553
ID: 11954591
not really sorry, i will try to find answer elsewhere.
0
 
LVL 10

Expert Comment

by:muso120999
ID: 11956345
I was actually going to suggest that you get your points refunded, and try a question from a different angle.

But thanks for the points.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

739 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