Solved

Need to run code in Unload Event

Posted on 2004-08-25
29
229 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

760 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

20 Experts available now in Live!

Get 1:1 Help Now