• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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)

0
Maxim10553
Asked:
Maxim10553
  • 13
  • 11
  • 5
1 Solution
 
muso120999Commented:
> 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
 
Maxim10553Author Commented:
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
 
muso120999Commented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Maxim10553Author Commented:
can you provide an example
0
 
muso120999Commented:
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
 
chrisevans123Commented:
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
 
chrisevans123Commented:
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
 
muso120999Commented:
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
 
chrisevans123Commented:
You can indeed - that's an old habbit of mine from flash 5 :)
0
 
muso120999Commented:
Having not actually tried it myself: does this solution actually work!?
0
 
chrisevans123Commented:
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
 
chrisevans123Commented:
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
 
muso120999Commented:
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
 
Maxim10553Author Commented:
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
 
Maxim10553Author Commented:
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
 
muso120999Commented:
> 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
 
Maxim10553Author Commented:
??
0
 
Maxim10553Author Commented:
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
 
muso120999Commented:
I can't help you without knowing what you are deleloping the exe in.
0
 
Maxim10553Author Commented:
sorry I guess I am not understanding. I am using Flash to create my application and also publish it into an exe
0
 
muso120999Commented:
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
 
Maxim10553Author Commented:
Would SWF Studio Work?
0
 
Maxim10553Author Commented:
I have access to getURL in SWF studio
0
 
muso120999Commented:
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
 
Maxim10553Author Commented:
okay i know you spoke briefly about GetURL in a comment above - can you discuss that option a little more?
0
 
muso120999Commented:
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
 
muso120999Commented:
Do you feel that we are making any progress here Maxim10553?
0
 
Maxim10553Author Commented:
not really sorry, i will try to find answer elsewhere.
0
 
muso120999Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 13
  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now