Starx Starx - 3 months ago 39
Javascript Question

How to detect slow internet connections?

Currently, most of the popular websites, like google, yahoo detect if the user connection speed is slow and then give a option to load basic version of the website instead of the high end one.

What are the methods available to detect slow internet connections?

P.S. I think this is achieved through javascript, so I will tag it as a javascript question? However, I am looking for answers oriented more towards PHP, if this is also server related.

Answer

You can start a timeout in an inline script block in <head>, which will be run as soon as it's encountered. You would then cancel the timeout when the load event fires. If the timeout ever fires, the page is loading slowly. For example:

<script type="text/javascript">
    var slowLoad = window.setTimeout( function() {
        alert( "the page is taking its sweet time loading" );
    }, 10 );

    window.addEventListener( 'load', function() {
        window.clearTimeout( slowLoad );
    }, false );
</script>

Obviously you would want to replace the alert with something a little more useful. Also note that the example uses the W3C/Netscape event API and thus won't work in Internet Explorer before version 9.