Cancel Flash Requests with JavaScript

    January 5, 2007

Internet Explorer can only make 2 connections at a time, which means that only two elements can be loaded while the rest is queued up.

Imaging a web page with 10 Flash movies, then only two of the movies can be loaded at the same time and the rest must wait until there is an available connection open.

That can be a real problem if you want to use the menu to navigate to another page on the website while the Flash movies are loading.

Because of the connection pool, you have to wait until you get an available connection before the page is requested.

What we want is a way to cancel all the pending requests and make sure we can navigate as fast as possible.

Unfortunately, there is no JavaScript function that will let us cancel requests, so we have to do something else. We can simply tell all the Flash movies not to proceed with the request by setting the movie property to an invalid value.

This should be triggered when a link in the menu is clicked.

Put this JavaScript in the section of the document:

<script type="text/javascript">

function CancelFlash()


  var elements = document.getElementsByTagName("object");

  if (navigator.appName != "Microsoft Internet Explorer")

   elements = document.getElementsByTagName("embed");

  for (i = 0; i < elements.length; i++)   {    if (typeof elements[i].IsPlaying() == "boolean")     elements[i].LoadMovie(0, "notexisting.swf");   } } </script>

Then add the onclick event to all the menu items like so:

<a href="products.aspx" onclick="CancelFlash();">Products</a>

It is not common to have many Flash movies embedded on the same web page, but in a recent case there were - a lot of charts - and they all load XML data dynamically created by a HttpHandler.

That takes a little bit of time and doubles the HTTP requests made to the server.

The solution is not bullet proof and you have to make sure that the tag has a classid attribute that tells the browser that it is a Flash movie. Then it should be cross-browser compatible.



Add to | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.