• Home
  • Om meg / About me
  • Fotogalleri
  • Kontakt meg
Blue Orange Green Pink Purple

Finding the best way to insert Flash

Posted in English, Webutvikling. on onsdag, juni 28th, 2006 by Håvard Pedersen
jun 28

In my earlier days (before I switched from Opera to Firefox, now I use the AdBlock Plus Firefox extension to block ads) I used to surf the web with Flash disabled. I was actually quite shocked to notice how many websites that just didn’t display anything when read though a browser without Flash support. This might be fixed by what I am about to discuss here, but it’s not my main point.

Have you ever tried validating a page with Flash in it? Chances are it didn’t validate. Even Adobes official material on how to embed Flash encourages use of the <embed> tag, which is not a valid tag in any of the W3C specs for markup languages on the web. It’s a Netscape invention, and it actually took quite some time until the Mozilla-based browsers managed to support the <object> tag in a satisfactory fashion. Frustrated with this fact, I started Googling, and this opened up a can of worms, as Internet Explorer didn’t fully support the <object> tag in a standard-conforming way either…

All I really wanted was a way that…

  • Was completely valid (X)HTML.
  • Sent the user to the Flash download center if the Flash plugin is missing or out of date.
  • Did not require JavaScript running in the browser in order to display the Flash. More and more people (mostly geeks) actually surf with JavaScript disabled.
  • Didn’t use the IE-specific conditional construct <!–[if !IE]> <–> (I don’t like browser-specific coding, even if it doesn’t break the standard).

The first solution I found was called Flash Satay by Drew McLellan. He basically started tweaking the object tag until all major browers managed to display the Flash content. While this sounds nice at first look, it does have several limitations. Internet Explorer won’t stream the Flash but insists on reading the entire contents before starting it. If the user doesn’t have Flash or an outdated Flash player, it just comes out blank. The last problem can be fixed by adding a blank “dummy” Flash file with a IE-specific construct that will do auto-install.

The second one was a piece of code by Ian Hickson that looked nice and clean, but unfortunately was very poorly documented (he doesn’t mention what browsers it has been tested in at all) and did use the IE specific comment-conditionals that I was set to avoid if at all possible.

The third (and last) returned solution was a small JavaScript thingy called SWFObject by Geoff Stearns. While my employer doesn’t have statistics on JavaScript usage among its users, I absolutely will not go for a solution that requires JavaScript in order to display anything. I’ve burned myself before on a similar choice before. ;)

But if you combine these, you will get a fairly nice setup. The big plus for SWFObject is that you define a <div> with content, and that content will be replaced with the Flash file if and only if SWFObject manages to do so. So if the <div> contains the Flash Satay method wrapped inside <noscript> I will have a nice fall-back for users without JavaScript. I’m also guessing that people likely to turn off JavaScript are likely to have an up to date Flash player anyway, if they haven’t disabled Flash as well.

And for people with Flash completely disabled, the <object> tag itself has a nice fallback builtin. The content of an <object> tag is rendered in place of the object if the object can’t be displayed. So any animated gif or textual message I want shown to those users is placed within the Flash Satay code which in turn is within the <div> that SWFObject operates on!

Example code (assumes swfobject.js is included in your header):

<div id="myflash_container">
  <script type="text/javascript">
    <![CDATA[
    // This is shown if we have JS support, but swfobject couldn't find a Flash plugin
    document.write('<div style="width: 150px; height: 150px;
background-color: #000000;">');
    document.write('<a href="http://www.adobe.com/go/gntray_dl_getflashplayer">');
    document.write('Get Flash player to view this content</a></div>');
    ]]>
  </script>
  <noscript>
    <!-- For browsers without JS support use Flash Satay for fallback -->
    <object type="application/x-shockwave-flash" data="testflash.swf" width="150"
height="150">
      <param name="movie" value="testflash.swf" />
      <div style="width: 150px;
height: 150px; background-color: #000000;">
        <a href="http://www.adobe.com/go/gntray_dl_getflashplayer">
          Get Flash player to view this content
        </a>
      </div>
    </object>
  </noscript>
</div>
<script type="text/javascript">
  var so = new SWFObject("testflash.swf", "myflash", "150", "150", "7", "#000000");
  so.write("myflash_container");
</script>

If you wonder why I have to use the <nocscript> tag the reason is simple. Otherwise Flash Satay would display the Flash content on load, only to have it instantly replaced with the same Flash by SWFObject. This would without doubt put some strain on the clients at load.

I haven’t tested this extensively, but Firefox 1.5 (with and without JS), Opera 8.5 (with and without JS), IE 5.0, IE 5.5 and IE 6 all showed the Flash in all its glory. If you have problems making it work, feel free to let me know.

Since most of the development at my current job is done in JSP, I will (in due time) make my own taglib for using this technique. Doesn’t <flash:insert name=”myflash” file=”testflash.swf” width=”150″ height=”150″ version=”7″ background=”#000000″ /> sound good to you too? ;) A small PHP function would probably take a couple of minutes to whip up.

Addendum: If you want SWFObject to work in IE4 and NS4 you need to add the following (before you include swfobject.js in the header is probably a good bet):

<script language="JavaScript" type="text/javascript">
  if(document.all && !document.getElementById) { /*ie4*/
    document.getElementById= function(id) {
      return(document.all(id));
    }
    document.getElementsByTagName= function(id) {
      return(document.all.tags(id));
    }
  }

  if(document.layers) { /*ns4*/
    document.getElementById= function(id) {
      return(document.layers[id]);
    }
  }
</script>
Del med verden:
  • Facebook
  • Google
  • E-mail this story to a friend!
  • TwitThis

5 Comments

  1. kel on juli 24th, 2006

    Interesting approach and does seem to handle JS being off while Flash plug is on, but the document.write code does not seem to validate for me using xHTML 1.0 Transitional. (plus, I’d already added the script “type” attibute as required) Thoughts?

  2. Håvard Pedersen on juli 24th, 2006

    Thanks for letting me now! Boasting a new standard-conforming way to insert Flash isn’t much if it doesn’t validate, is it? :P

    I fixed both script-tags now as well as the issue with the first script tag. The reason is that XML (which is what XHTML essentially is) allows any tags within others, so the validator (and in theory, any browser) tried to parse the HTML code within the script tag. The CDATA construct I inserted prevents this from happening. _Now_ it finally validates! ;)

  3. Ruben Garcia on oktober 23rd, 2006

    I use this code in my websites, it validates all the time. the url for this site is http://www.telmendez.com

  4. Michael Richards on juni 4th, 2007

    I’m having some problems with it.

    to build up a page i9 have 2 files they are Header.php and Footer.php i include them at the top and bottom of a new page and put the content in between…

    i have put

  5. Håvard Pedersen on juni 4th, 2007

    It’s difficult to tell, but I’m quite sure the problem isn’t in my code. It’s impossible that tags parsed by the browser when displaying the page affects how your serverside include scripts behave.

    I’m guessing you did some typos when pasting my code. Looking at the generated sourcecode from the browser might help you.



Leave a Reply

Håvard Pedersen

  • Kategorier
  • Biografi (kortversjon)

    Håvard Pedersen er en webutvikler ved Nasjonalt Senter for Telemedisin. Hans interesser inkluderer brukervennlighet, tilgjengelighet, webutvikling, nye medier, nettsamfunn og markedskrefter på internett.

    I tillegg er han 32 år gammel, forlovet, parantesjunkie, smileymisbruker og familiefar.

    Forbehold: Meninger jeg gir uttrykk for i denne bloggen er mine egne, og må på ingen måte tillegges min arbeidsgiver.

  • Mine identiteter
    Fuzzy76 @ Twitter
    Håvard Pedersen @ Facebook
    Håvard Pedersen @ LinkedIn
    haavard.pedersen @ GMail
    havard<a>flashmail·com
    @ MSN

  • Recommended Reading
    • Cyanide and Happiness
      published today
      By Daily Comics: Cyanide and Happiness
    • Circling the big domino
      By Seth's Blog
    • The Fallacy of Page Zooming — All in the head
      By allinthehead.com
    • Nemi (bt.no)
      published Mon 8 Jun 2009
      By Daily Comics: Nemi (bt.no)
    • What the Heck is Social News?
      By Baekdal.com
  • Archives
    • april 2009
    • mars 2009
    • februar 2009
    • oktober 2008
    • juli 2008
    • juni 2008
    • mai 2008
    • april 2008
    • mars 2008
    • januar 2008
    • desember 2007
    • november 2007
    • oktober 2007
    • september 2007
    • juni 2007
    • mai 2007
    • april 2007
    • mars 2007
    • februar 2007
    • januar 2007
    • desember 2006
    • november 2006
    • oktober 2006
    • september 2006
    • august 2006
    • juli 2006
    • juni 2006
    • mai 2006
    • april 2006
    • mars 2006
    • februar 2006
    • januar 2006
    • desember 2005
    • november 2005
    • oktober 2005
    • september 2005
    • august 2005
    • juli 2005
    • juni 2005
    • mai 2005
    • april 2005
    • mars 2005
    • februar 2005
    • januar 2005
    • desember 2004
    • november 2004
    • oktober 2004
    • september 2004
    • juli 2004
    • juni 2004
    • mai 2004
    • april 2004
    • mars 2004
    • februar 2004
    • januar 2004
    • oktober 2003
    • september 2003
  • Search






  • Home
  • Om meg / About me
  • Fotogalleri
  • Kontakt meg

© Copyright Håvard Pedersen. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top