Link to home
Start Free TrialLog in
Avatar of Maxim10553
Maxim10553Flag for United States of America

asked on

Need to run code in Unload Event

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)

Avatar of muso120999
muso120999

> 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.
Avatar of Maxim10553

ASKER

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!
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.
can you provide an example
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
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

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.
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)
You can indeed - that's an old habbit of mine from flash 5 :)
Having not actually tried it myself: does this solution actually work!?
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
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.
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
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
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
> 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
??
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.
I can't help you without knowing what you are deleloping the exe in.
sorry I guess I am not understanding. I am using Flash to create my application and also publish it into an exe
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.
Would SWF Studio Work?
I have access to getURL in SWF studio
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.
okay i know you spoke briefly about GetURL in a comment above - can you discuss that option a little more?
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.
ASKER CERTIFIED SOLUTION
Avatar of muso120999
muso120999

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
not really sorry, i will try to find answer elsewhere.
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.