Forcing XML Refresh in ActionScript 2.0

I’ve been working with Flash since version 4 but only came across this trick a few weeks ago. Normally, when using the XML object in ActionScript to load data into your application, if the XML file has been cached, Flash will not reload updated XML data until the cache is cleared or expires. For many applications I’ve worked on, this has been a small issue as data can be updated many times during the day. To force Flash to always retrieve a new version of the XML, you must append a unique identifier to the URL in the load method. For instance, using getTimer(), you can say:

var myXML:XML;
myXML = new XML();
myXML.ignoreWhite = true;
myXML.load("http://somewebsite.com/data.xml?"+getTimer();

9 thoughts on “Forcing XML Refresh in ActionScript 2.0”

  1. You can use a similar method in the embed code used in the HTML/PHP, etc used to put the SWF on the page. Appending the “.swf” file name with a query string and changing variable will ensure the newest swf is passed to the viewer rather than a cached one.

    eg( blahblah.swf?myVariable=randomNumber )

    Great for testing purposes.

  2. Nice. Thanks for the tip. It’s really amazing that there are still so many small things to learn even after working with a specific platform for years on end. I wonder if this would work for loading images and such through a MovieClipLoader object as well?

  3. I have a big problem with this and my action script knowledge is not very well, so please, can you tell me where to put this code.

    I have a component in flash and in action script is:
    var lis:Object = new Object();
    lis.onDoubleClick = function(eo:Object){
    getURL(eo.link, “_self”);
    }

    var lis = {};
    lis.onSelectPhoto=function(evt){
    desc_txt.text = evt.desc;
    }
    myPhotoFlow.addEventListener (“onSelectPhoto”, lis);

    XML file generates dynamically from mysql/php, site: http://www.hrs.hr

    Thanks

  4. I was doing a comeplete redesign of my site adding a bit of flash, and did not consider the refresh a problem, and had several pics in a slideshow. I realized the problem with refresh when I removed all the ‘extra’ pics. I decided to google it, and your page was the second shown. It worked like a charm!! And so simple a solution. Thank You!

    P.S. Just for others, the code above needed an extra ) in the line to work for me. The flash would not load the .xml file locally in my computer and gave me an error, but worked perfect online. The line I used was:
    images_xml.load(“slide/slide.xml?”+getTimer());

  5. I’m having exactly the same problem… xml is not being refreshed. I’ve tried the above fix (and several others that I’ve found in my search) to no avail. Basically, I have a site that includes a photo gallery. The gallery is broken down by country. When the visitor clicks on a country, a new xml file is generated from my database with the appropriate photo information.

    No matter what I do, I can’t get the xml to refresh. I can see that the xml is being generated properly each time the user clicks a country, but I always end up with pictures from the first country I clicked on. If I bring the xml up in a different browser window, then the next click on a country (no matter what country it is) displays the xml in the other window. To see it in action, go to http://www.markmardon.com/markmardon3.html. To view the xml file, go to http://www.markmardon.com/photos.xml.

    Any help you could provide would be great.

  6. Thanks so much for publishing this! And thanks too to Thomas McAdams without whom I may well have torn all my ahir out all over again!

Leave a Reply to Retard Cancel Reply

Your email address will not be published. Required fields are marked *