the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • JavaScript Speed Detection

    Dec 14 2005

    This question came up on the css-discuss mailing list today (asked by Howard 'duke' Holtz):

    I would like to include a Flash header on the homepage, but switch to a standard gif header for viewers with slow connections. I was hoping that there is a way to maybe have two “includes”, and use one or the other – but first I need some kind of 'conditional switch' or 'logic switch'.

    This is a great question. I don't think any website has addressed this issue through scripting. We've all seen websites that start off with "Click here for Flash, Click here for slow connections" pages. But what about using JavaScript to test the speed of a connection?

    I started experimenting with JavaScript and came up with a fairly simple, clean solution. It is all based on the onload event of an image. What we will do is time how long it takes to load an image. If the time is below our threshold for "high speed", we will display the flash content. Otherwise, we will do nothing and display the default content.

    <script type="text/javascript">
    var start = (new Date()).getTime();
    var THRESHOLD = 2500; // a number we will calculate below
    
    function speedTest() {
        var duration = (new Date()).getTime() - start;
        if (duration < THRESHOLD) {
            // fast connection (display flash)
        } else {
            // slow connection (do nothing?)
        }
    }
    </script>
    <img src="some-image.jpg" onload="speedTest()">
    

    The onload event can go on any image on the page, preferably the largest image to give us a better estimate. Let's say we only want users downloading faster than 10k/s to get the flash code. We check the size of our some-image.jpg, and see that it is 25kb. We can easily calculate the THRESHOLD value like so: (25kb) / (10k/s) = 2.5s. Converting this to milliseconds gives us 2500, so this is what we will use for the THRESHOLD.

    If you're using Bobby VanDerSluis' Unobtrusive Flash Objects 2.0, you can put the UFO code to load Flash in the block for users with a fast connection. You can then do nothing for users with a slow connection and just let them see the default content on the page.

    I have thought of one problem with this solution: if the image is in the user's cache, the number will be very low. This will cause the flash to be loaded if they come back later. To avoid this, you could add a no-cache rule on the webserver for this image. Or, instead of using an image on the page, you could load an image in the background with JavaScript using a random URL like so:

    var testImage = new Image();
    testImage.onload = speedTest;
    testImage.src = "some-image.jpg?random=" + Math.random();
    

    Of course, if you are using PHP or another scripting language on the server, you could generate the random URL a number of different ways. This fix also has the downside that the visitor must download this extra image every time. You'll have to balance these factors in your solution to come up with one that fits.

    Good luck! I'd love to hear your comments, suggestions and questions about this. Let me know how it goes on your own web sites!

    Tags: javascript javascript speed flash scripting
    View 11 Comments | Add a comment
  • Comments

    1. Mikhail Bozgounov at 2:01pm on December 15, 2005

    ...should try it one of these days.

    Could this be done through PHP too?

    Just an idea...

    And thx for sharing this! :-)

    2. Jesse Skinner at 6:18pm on December 15, 2005

    Ya, it should totally be possible with PHP. I guess it would work more or less the same way.. sending a file, checking how long it takes, and perhaps setting a SESSION variable with the viewing-mode.

    I'm just packing to go back to Canada for the holidays, so somebody else will have to write up the PHP solution :)

    Happy Holidays,
    Jesse

    3. haribol at 12:23am on November 29, 2007

    tahank you for this script

    4. Damien at 5:40pm on January 10, 2008

    Nice idea!

    5. Rob at 9:28pm on February 21, 2008

    Does image size matter?

    6. nG at 4:33am on April 20, 2008

    I have been thinking about this as well. The only issue is using an image. The user has to load the image... that is the only major problem I see with this. Adding a timeout would help make it better, but I still cringe at the thought of making them load a file to  test the speed.

    7. Rob at 4:44pm on April 21, 2008

    I think using an image for the test is standard, but I'm no expert. I know that sites like pcpitstop.com uses images to test the speed. My boss uses a service that detects connection speeds, I wonder how they work...?

    8. Alex Le at 8:52am on April 22, 2008

    Based on this article, I wrote a more completed solution to use javascript for speed detection on my blog

    http://alexle.net/archives/257

    Cheers!

    Alex

    9. MooCow at 4:20pm on May 7, 2008

    I have a high-speed connection and I still don't want to see anything that's made in Flash. In fact I browse with disabled plug-ins because Flash is an annoyance in 99.9999999% of cases.

    Don't assume that high-speed = Flash and low-speed = HTML/CSS.

    10. Rob at 4:24pm on May 7, 2008

    MooCow...I somewhat agree with you. Being a frequent visitor of sites like espn.go.com, they WAY over do it with flash, but with a high-speed connection, I can't see how that can be such an annoyance. Nonetheless, you're right. I never believed in the thought of having a high-speed version of a site as well as a low-speed. That's just too much.

    11. Jesse Skinner at 4:28pm on May 7, 2008

    @MooCow, @Rob - I agree as well. In a perfect world, all sites would be low-speed, and people would click specifically to see large things (videos, games, etc.)

    The worse thing is, most Flash sites don't even give a choice, because they don't even have a non-Flash version. Say goodbye to search engine optimization and accessibility!

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • JavaScript Functions are Variables
    • See All...
  • Categories

    • javascript (37)
    • links (19)
    • about (18)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites