The trick to disabling the browser from loading your flash movies from its cache is to append a unique ID to the movie. Today we'll take a look at how a small piece of JavaScript code can force a new download. We will also look at how this trick has been misused by appending variables through the source instead of using FlashVars.
In the following example we create a variable, uniqueID, who will reference a unique string that we can append to the source of our flash movie. There are a few ways too define a unique string and/or number, like a random number from 0 to 1000, but I like to use JavaScript's Date(); object. It returns the date when the page was rendered; Tue Jun 28 2007 3:16:51 GMT-0400 (EDT).
-
<!DOCTYPE html
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>SWF: Disabling Cache</title>
-
<!-- SWFObject embed by Geoff Stearns http://blog.deconcept.com/swfobject/ -->
-
<script src="swfobject.js" type="text/javascript"></script>
-
</head>
-
<div id="flashcontent">
-
Place alternate content here
-
</div>
-
-
<script type="text/javascript">
-
var uniqueID = Date();
-
var so = new SWFObject("flashmovie.swf?id=" + uniqueID, "movie", "300", "200", "6");
-
so.write("flashcontent");
-
</script>
-
</body>
-
</html>
We begin by creating our uniqueID variable, var uniqueID = Date();. Next, we append that variable to the source inside the SWFObject constructor "flashmovie.swf?id=" + uniqueID.
-
var uniqueID = Date();
-
var so = new SWFObject("flashmovie.swf?id=" + uniqueID, "movie", "300", "200", "6");
Now that we have our uniqueID, the browser will no longer look for flashmovie.swf but flashmovie.swf?Tue Jun 28 2007 3:16:51 GMT-0400 (EDT). Thanks to the limits of time travel, the next time your page is rendered, the browser will look to download flashmovie.swf?Tue Jun 28 2007 3:18:24 GMT-0400 (EDT).
Where people have unintentionally forced a new download is when passing variables through the source instead of using FlashVars. Here is an example, using the same code from above, on how we would pass the users score to our flash movie while ensuring the movie is cached.
-
var so = new SWFObject("flashmovie.swf", "movie", "300", "200", "6");
-
so.addVariable("user_score", "<% user_score %>");
The incorrect way is to append it to the SWF's source because appending has two functions; first, it adds to the source's name and two, it passes the variable to your movie. Now, this still does the same as the above code but when the users score changes, it will download a new SWF movie.
-
var so = new SWFObject("flashmovie.swf?user_score=<% user_score %>", "movie", "300", "200", "6");



