The First and Only Magazine for Macromedia MX™
   
 
Notes from the Flash, Flex and ColdFusion trenches

Category List

Quick Poll

Where are you located?
North America
South America
Europe
Asia
Africa
Australia

My RSS Feeds








Hit Counter

Total: 1,350,997
since: 20 Jan 2005

Search Box

 

Search The Web

Google

MAX 2008

Fun Stuff

Mailing List

Got A Question?

Got A Question?

Leave a comment by the appropriate Entry, or email me

Tag Cloud

                                                                                                                       

Loader Component and Internet Explorer

posted Thursday, 20 January 2005

On a recent project, I found an oddity with the loader component.  I found some images loaded with it were having their complete event fired before the image was actually done loading.  This caused all sorts of problems, as I was dynamically resizing a frame around the loaded content in the complete event.  With the event firing prematurely, the height and width of the content were showing as 0,0 so the frame was not being properly sized.  It seems the issue lies with the mx.core.ExternalContent class which is used by mx.core.View (the superclass of Loader).

ExternalContent has a method checkLoadProgress which reads like this:

function checkLoadProgress():Void
 {
  var i:String;
  for (i in loadList)
  {
   var x:Object = loadList[i];
   //trace("loading..." + i);
   x.loaded = x.obj.getBytesLoaded();
   x.total = x.obj.getBytesTotal();
   //trace( x.loaded +"/"+ x.total );
   if (x.total > 0)
   {
    x.obj._visible = false;
    dispatchEvent({type: "progress", target: x.obj, current: x.loaded, total: x.total});
    if (x.loaded == x.total) {
     if (loadedList == undefined)
      loadedList = new Object();
     loadedList[i] = x;
     delete loadList[i];
     doLater(this, "contentLoaded");
    }
   }
   else if (x.total == -1)
   {
    // sometimes you get a -1 before it starts loading
    if (x.failedOnce != undefined)
    {
     x.failedOnce++;
     if (x.failedOnce > 3)
     {
      dispatchEvent({type: "complete", target: x.obj, current: x.loaded, total: x.total});
      //trace("total == -1 loaded = " + x.loaded);
      delete loadList[i];
      delete x;
     }
    }
    else
     x.failedOnce = 0;
   }
   else
   {
   }
   doLater(this, "checkLoadProgress");
  }
 }

The problem is the line "if (x.failedOnce > 3)"  whats happening here, is that if after 3 tries the content still hasn't started loaded (therefore the getBytesLoaded() call returns -1), this component fires its complete event, even though it hasnt actually started.  I dont know why this is being done, but clearly, its causing problems.     

There are several possible solutions, including one mentioned on bgxcomponents.com, which uses the prototype method to override the component.

Another less desirable solution would be to change the code in the ExternalContnet class.  this is clearly a bad solution, as this code was probably written for a reason, and just because we dont understand why, doesnt mean its not valid for some cases. 

The OO friendly solution would be to subclass Loader, and override the method for our new subclass.  I've done this, the code can be seen below.

class IELoader extends mx.controls.Loader{
  static var symbolName:String = "IELoader";
  static var symbolOwner:Object = IELoader;
  var className:String = "IELoader";
  var loadList:Object;
  var loadedList:Object;

  function checkLoadProgress(){
  
   for (var i in this.loadList){
     var x:Object = this.loadList[i];
   
     x.loaded = x.obj.getBytesLoaded();
     x.total = x.obj.getBytesTotal();
     
     if (x.total > 0){
       x.obj._visible = false;
       this.dispatchEvent({type: "progress", target: x.obj, current: x.loaded, total: x.total});
       if (x.loaded == x.total) {
         if (this.loadedList == undefined){
           this.loadedList = new Object();
         }
         this.loadedList[i] = x;
         delete loadList[i];
         this.doLater(this, "contentLoaded");
       } else {
         this.doLater(this, "checkLoadProgress");
       }
     } else {
       if (x.total == -1){
         if (x.failedOnce != undefined){
           x.failedOnce++;
         } else {
           x.failedOnce = 0;
         }
       }
       this.doLater(this, "checkLoadProgress");
     }
   }
 }
}

Remember, this is a class file for a new Component.  To effectively use it, it needs to be linked with a movie clip, which will act as your component.  For ease of use, I've uploaded an installer which contains this class packaged as a Flash component.  You can download it from here.

tags:  

links: digg this    del.icio.us    technorati    reddit




1. a reader left...
Saturday, 16 April 2005 5:43 pm

hey thanks for this!

steven lyons


2. Jeff Tapper left...
Saturday, 16 April 2005 5:50 pm

as always, glad to be a help!


3. a reader left...
Friday, 27 May 2005 1:47 pm

You've outdone yourself on this one! The component works perfectly. Macromedia needs to thank you for fixing they're software...

Jon Lawpaugh


4. chrillo left...
Monday, 18 July 2005 8:59 am

I have been struggeling around with this problem all day...without any success until i found your component...its works perfectly fine!!

thank you soo much, you saved my day


5. Adrian Showalter left...
Friday, 26 August 2005 5:38 pm

For the past three days all that I have done is to search for a solution to this problem!!! Thanks for providing such a dynamite solution. I've run into this problem many time in my application development, and you're the first person who's provided a real, workable solution. Thanks a million!!!


6. Weber Philippe left...
Monday, 29 August 2005 8:08 am

Thank You very mutch for this solution. Like all others I went crazy! Thanks again! Phil from Switzerland


7. Lorne left...
Wednesday, 2 November 2005 1:10 pm

Now what kind of component fires "complete" when it's not complete? Go figure...

Anyway, you rock ;)


8. mik left...
Wednesday, 2 November 2005 9:30 pm

it might be an overstatement to say that you saved my life, but only slightly. you did save my sanity. i have spent the last day and a half a) trying to figure out why ie was blowing apart my project, which worked great on every other browser, and b) honing the search terms to describe the problem until i finally arrived here.

thank you.


9. conidis left...
Monday, 7 November 2005 7:15 am :: http://www.hellasinternet.com

wow! good work! that's why Firefox was the only browser running my loaders properly. MX 2004 help says about Loaders: "If you load certain version 2 components into a SWF file or into the Loader component, the components may not work correctly. These components include the following: Alert, ComboBox, DateField, Menu, MenuBar, and Window." Well.. they work now!


10. Sean left...
Wednesday, 16 November 2005 3:36 pm

I cant tell you how many hours I have wasted over the past year or so trying to fix IE and the loader component. I had even given up on using it, because of this bug - although I thought it was IE's fault, and not Macromedia's.

Thank you from the bottom of my heart for this solution.


11. Steve Carpenter left...
Friday, 25 November 2005 4:36 pm :: http://blogs.warwick.ac.uk/stevencarpent

I second that! I've just spent 3 days trying to get to the bottom of a Loader problem - this has fixed it completely - a thousand thanks! :-)


12. Vahe Shahinian left...
Monday, 9 January 2006 1:11 pm

Wow, this is perfect. I am amazed MM did not fix this in their "Flash 8" release.

Thank you for providing the component. You should be charging us an arm & leg for it :)

  • Vahe


13. Sander left...
Wednesday, 1 February 2006 7:38 am

Indeed, MM should have fixed it in F8.

Thanks a million!


14. George left...
Monday, 6 February 2006 10:14 am

I have the same problem but with the ScrollPane Component. I guess the the ScrollPane uses the Loader component to load the content, and i must change this with IELoader. But how?


15. Jeff Tapper left...
Tuesday, 7 February 2006 5:35 pm

Actually, both Loader and ScrollPane extend View, and its View that has the mixins from the ExternalContent class. The real bug is in ExternalContent, so this can potentially be seen by any subclass of View. You can use the same fix I list above, but you will want to subclass ScrollPane instead of Loader. Something like this.

class MyScrollView extends mx.core.ScrollView{
  static var symbolName:String = "MyScrollView";
  static var symbolOwner:Object = MyScrollView;
  var className:String = "MyScrollView";
  var loadList:Object;
  var loadedList:Object;

  function checkLoadProgress(){
  
   for (var i in this.loadList){
     var x:Object = this.loadList[i];
   
     x.loaded = x.obj.getBytesLoaded();
     x.total = x.obj.getBytesTotal();
     
     if (x.total > 0){
       x.obj._visible = false;
       this.dispatchEvent({type: "progress", target: x.obj, current: 
x.loaded, total: x.total});
       if (x.loaded == x.total) {
         if (this.loadedList == undefined){
           this.loadedList = new Object();
         }
         this.loadedList[i] = x;
         delete loadList[i];
         this.doLater(this, "contentLoaded");
       } else {
         this.doLater(this, "checkLoadProgress");
       }
     } else {
       if (x.total == -1){
         if (x.failedOnce != undefined){
           x.failedOnce++;
         } else {
           x.failedOnce = 0;
         }
       }
       this.doLater(this, "checkLoadProgress");
     }
   }
 }
}


16. alain left...
Tuesday, 14 March 2006 2:25 pm

one more thanx to you you're good you, you're good... :-)


17. Douglas left...
Monday, 17 April 2006 8:53 pm

Solved a big headache! And I thought I was doing something wrong. @_@; Thanks a million!


18. Maikel Sibbald left...
Thursday, 11 May 2006 5:29 pm

Thanks, but I got a problem in FireFox. I'm using Image component in a CellRenderer of a TileList (Flex 1.5). What is does is reusing the tiles while scrolling. therefor the content of the Image is changed everytime on scrolling. The Effect in FireFox is that every request is shown!!! So the image is flickering untill the last image is reached. No problem in IE it works fine. If you need more info contact me..


19. Jeff Tapper left...
Thursday, 11 May 2006 5:38 pm

Maikel -

Sorry, sounds like your issue has nothing to do with the bug in the Loader component, but the inherent nature of cell renderers. Try adding conditional logic to your setValue method, to only load the image if its not the same. Something like:

function setValue(suggested:String,item:Object,selected:String):Void {
	if(item.someid==undefined){
		this.imageID=undefined;
	} 
	if (item!=undefined && (this.imageID!=item.someid)){
		this.image.load(item.imageURL);
		this.imageID=item.someid;
	}
}


20. Joe T left...
Friday, 26 May 2006 12:05 pm

Jeff, Thanks a million. This loader component allowed me to leave some hair on my head! Regards


21. Petre left...
Wednesday, 31 May 2006 6:40 am

You saved my mental health ! zilion thanks !!


22. jakob left...
Saturday, 3 June 2006 9:42 am

Hi Jeff, I can see that IELoader works perfectly, but how can you override a method from a class (ExternalContent) that isn't a supertype of IELoader?


23. Jeff Tapper left...
Tuesday, 6 June 2006 7:44 am

Jakob -

An excellent question. The reality is that the ExternalContent class is a "mixin" which means that its properties and methods are added to the class which is using it. In the case of the Loader, the mx.core.View class mixes in the functiality of the ExternalContent class, so those methods and properties become available as methods and properties of View. As View is the superclass to Loader, the mixed in methods are also methods of the Loader class. Since they are available as methods of the Loader class, we are free to override those methods in our subclasses.

I hope this explanation isnt too incoherent, I havent had my morning coffee yet.


24. Osama Mortada left...
Saturday, 17 June 2006 5:16 am

Thank you very very much, I was struggling for more than 2 months around this issue.

Now it works perfectly, I recommend the opinion that macromedia has to thank you for debuging their products.


25. tppvideo left...
Monday, 19 June 2006 1:14 pm :: http://www.yourbackupvault.com/ybv2

I've been struggling with the scrollpane properly loading its content with IE. On firefox it loads the content perfectly, but in IE instead of masking the content page, it is placed in front if everything else. http://www.yourbackupvault.com/ybv2

Any help would be greatly appreciated.

Richard


26. John Nickles left...
Thursday, 20 July 2006 11:18 am

@ tppvideo

I'm having the same problem. When I load a swf file into the scrollPane it loads it in the correct position but outside of the scrollPane. BUT if you click on a button to reload the swf into the scrollPane again it loads the external swf into the scrollPane perfectly and does so again and again unless you clear the IE cache.

Of course it works perfectly in Firefox >:/

Why wont it load correctly the first time?


27. tppvideo left...
Thursday, 20 July 2006 11:52 am

Hello John,

Basically I found the problem to be a timing issue due to the size of the *.swf file being loaded. The scrollPane component was loading before the *.swf file thus the problem. Mozilla just handles it, however IE doesn't.

I solved the problem by purchasing a different scollPane component provided by ghostwire (http://ghostwire.com/), swaped their component in my project and volia problem gone.

Hope this helps.

--- Richard Ybarra Twin Palms Productions, Inc. http://www.tppvideo.com


28. John Nickles left...
Thursday, 20 July 2006 7:49 pm

Hey! Thanks for the heads up! I've spent many hours trying to figure out what the problem was.

You can see my efforts and frustration here:

http://flashmove.com/forum/showthread.php?t=26297

I'll dig a little more around the WWW to find a fix to this problem and if I don't find one then I'll give up and switch all the scrollPanes to plain old text boxes :/

Good to see a fellow media guy on the forums,

John Nickles :: webmaster :: wealthtv.net :: nicklesconsulting.com


29. tppvideo left...
Thursday, 20 July 2006 7:56 pm

I certainly understand that, I spent more hours than I even want to think of trying to figure it out. Here is a link from the Flash site which contains a possible solution let me know if it helps:

http:// www.adobe.com/cfusion/knowledgebase/index.cfm?event=view&id=KC. 17226748&extid=17226748&dialogID=32452358&iterationID=1&sessionID=48301e b9f54e7e4b44b3&stateID=0+0+32454646&mode=advanced

--- Richard Ybarra Twin Palms Productions, Inc. http://www.tppvideo.com


30. John Nickles left...
Friday, 21 July 2006 10:41 am

The URL wouldn't load (probably because of the session ID). I'm curious to know what knowledgebase article you found.


31. tppvideo left...
Friday, 21 July 2006 10:51 am

Adobe TechNote Details ID: 17226748 Product(s): Flash Player, Flash Versions: MX 2004 OS: NA Browser: Internet Explorer Server: NA Database: NA

--- Richard Ybarra Twin Palms Productions, Inc. http://www.tppvideo.com


32. kim left...
Friday, 21 July 2006 8:56 pm

THANK YOU! Thank you so much for writing this. I thought it was just me! I've had that little bug haunting me for months; now I can finally make my app BUG-FREE! Rad!


33. Chris left...
Monday, 14 August 2006 5:59 pm

Hey Hey! Wonderful work! I was also having troubles doing cross-browser testing - you have made my life 10 x easier already!!

Thanks a million!


34. Chris left...
Monday, 14 August 2006 5:59 pm

Hey Hey! Wonderful work! I was also having troubles doing cross-browser testing - you have made my life 10 x easier already!!

Thanks a million!


35. Jacob Berendes left...
Tuesday, 22 August 2006 6:46 pm :: http://www.jacobberendes.com

I have been fussing with this issue for almost a month now. Thank you a thousand times over for finding this bug.


36. Bruce Talcott left...
Tuesday, 5 September 2006 6:49 pm

Thank you very much!!!!!!. Wonderful work!!!!


37. mark left...
Thursday, 21 September 2006 12:07 am

you have saved my life with this. ive been trying to figure this out all day


38. Erin left...
Thursday, 5 October 2006 2:05 am

you are awesome

thanks


39. Jorge Almério left...
Wednesday, 11 October 2006 9:41 am

Please, I newbie in Flash and I could not solve the problem with the scrollPane component, even try to use the Class suggested by Jeff Tapper.

I don't know how to use the code.

For the Loader component I used the prototype method from bgxcomponents.

I also found a link to a ScrollPane fix, but it is a broken link: www.nordform.org/scrollpane_fix/BgxScrollPaneFix.as


40. Jeff Tapper left...
Thursday, 12 October 2006 2:58 pm :: http://jeff.mxdj.com

Jorge -

Unfortunately, im too busy to write this for you. You should be able to follow my lead from this component for the ScrollPane pretty easily. If i ever get around to a similar workaround for hte scrollPane, I'll be sure to post it.


41. Jorge Almério left...
Monday, 16 October 2006 3:28 pm

Ok, anyway, thank you very much.


42. Michael left...
Wednesday, 22 November 2006 2:15 pm

Dear Jeff,

You are probably sick to death of hearing this, but bravo on the code. This seriously helped me out. It is people such as yourself, who post fixes like this, that make the Internet such a great resource for developers. An excellant solution to the problem and an informative lesson on overriding.

Many thanks.


43. NA left...
Thursday, 21 December 2006 8:10 am

hey will this fix the problem i have on my site??

http://www.naphtaliarts.com


44. Raj Choudhury left...
Wednesday, 24 January 2007 11:20 am

Just have to say Big Thanks. Been losing sleep over this problem for ages. This is fantastic!! Thanks


45. Toloot left...
Monday, 29 January 2007 10:55 pm

Thanks a lot. Fortunately, I only speed about an hour before finding this code.


46. Phasefire left...
Friday, 16 March 2007 7:08 am :: http://www.phasefirefilms.com

I've spent about three hours trying to fix this since I assumed it was something wrong with how I was handeling events. Finally I checked google and found you. Thank you you are a golden god of awesomeness!


47. Rytis left...
Friday, 16 March 2007 12:06 pm

Thanks a lot! This bug fix saved my ass :)


48. Golem left...
Saturday, 31 March 2007 8:00 am

FAAAAAANTASTIC !!!!!!!!!


49. michiel left...
Friday, 11 May 2007 11:08 pm

Thanks Jeff for your excellent help!

I cannot believe that Adobe still hasn't even fixed their AS2 Loader (I'm just hoping it is fixed in the new AS3 UILoader) in their latest Flash SC3 release I'm using, years after the problem appeared in the first place.

But then again, they seem to have left more things for improvement...


50. R Goldbach left...
Monday, 9 July 2007 1:02 am

GREAT CONTRIBUTION! I was loosing my sleep with the wrong IE positiioning and resiziing problem of the mx.controls.Loader up till I found your post.

Thank you very much for the MVP file, solved it for good!

Regards,

Ricardo


51. Milan left...
Monday, 6 August 2007 1:58 pm :: http://www.softlakecity.com

Wooohoo! Finally it works in IE just like it does in Firefox. I couldn't believe it can be beacuse of a bug but luckily a friend of mine showed me your post. THANK YOU And for the installer: THANK YOU


52. mix left...
Monday, 6 August 2007 2:01 pm :: http://www.my-birthday.info

You are my hero. I just wish I had found your page 24 hours ago.


53. Ariel left...
Thursday, 23 August 2007 6:09 am

Wow... This is awesome.


54. DoeBoye left...
Friday, 21 September 2007 9:15 am

All I can say is Wow!

At least half a day spent trying to figure out why in IE my external jpgs were not resizing in the loader the first time they were loaded, and the progress bar was not working at all, and then I come across this beautiful little gem!

Thank you so much for saving my sanity!


55. Alex left...
Monday, 10 December 2007 9:01 am

thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you

ok,ok, too much, it's becoming kind of gayish... anyway, you saved my job!


56. James left...
Tuesday, 1 January 2008 6:03 pm

Years has this one been getting me; finally glad to find out it wasn't shabby coding on my part!


57. Fredrik left...
Tuesday, 22 January 2008 11:38 am :: http://labs.kaliko.com

Any idea how to fix this for Actionscript 3.0 applications as they seem to have a simular problem with both Loader and the UILoader objects in Internet Explorer?


58. Fredrik left...
Wednesday, 23 January 2008 4:20 am :: http://labs.kaliko.com

I guess the same thing could be done in AS3, but I wasn't able to. However I got another solution working. I've posted it on my site if anyone is interested: http://labs.kaliko.com


59. zord left...
Saturday, 16 February 2008 9:21 am

thank you very much! it's working perfectly :)

i'm pretty newbie in flash. i started using it, cause i thought it's a real-cross-platform way of programming... and i almost cancelled flash, because this bug :D


60. Joseph left...
Wednesday, 2 July 2008 12:23 pm

This is great, it saved a bunch of braincells. Can I uh... send you flowers or something?


61. Jeff Tapper left...
Wednesday, 2 July 2008 12:40 pm :: http://blogs.digitalprimates.net/jefftap

No need for flowers. I wrote this a few years ago when i first ran across the problem, and am glad to share the solution with anyone who needs it.


62. DeePoP left...
Wednesday, 9 July 2008 9:09 am

Thank you very mutch for this solution.

DeePoP


63. Jeremy Wiggins left...
Friday, 8 August 2008 3:35 pm

Awesome, THANKS! You kept me from pulling the rest of my hair out.