4

I fear I already know the answer, but I might as well risk it: Does anyone know if there is some way I can test whether a client that is accessing a website is a thin client (desktop terminal) or not? The issue here is that thin client start to lag horribly if you even consider using JavaScript(animation).

I basically want to serve a "light" version of the website, where I disable all special effects depending on the client. I have looked at the user agent but that doesn't seem to give away anything useful.

Is there any way to tell?

Some one else
  • 325
  • 1
  • 3
  • 17

2 Answers2

2

There's no really clean way to solve this, because there's no such thing as a HTTP header for thin clients or remote desktops.

But if you really need a solution to identify slow clients, you can try the following. It's similar to what Google uses on Maps to determine if a client can handle Maps.

In this approach, you would deliver the HTML page with a chunk of embedded JavaScript. This chunk performs a resource-heavy operation which should be similar to what your actual code does.

You measure the time consumed for this operation and decide if it's performing well or not.

Now you load the actual JavaScript, either by creating the script tag with document.write and passing a parameter which tells the script in which mode to run, or by initializing the already loaded code with an appropriate parameter.

A quick'n'dirty example implementation would look like this (using jQuery, but can as well be implemented in plain JS):

<script type="text/javascript">
    var
        $elem,
        now = new Date(),
        isFast,
        counter = 0;

    while (new Date() - now < 100)
    {
        $elem = $('<div style="display: none">');
        $('body').append($elem);
        $elem.remove();
        ++counter;
    }

    alert(counter);
    isFast = (counter > 100);


    // now, either embed the actual script ...
    document.write('<scr'+'ipt type="text/javascript" src="http://www.example.com/js/test.js?fast=' + isFast + '"></scr'+'ipt>');

    // ... or initialize the already loaded code
    var myControllerInstance = new MyController(isFast);
    myControllerInstance.makeStuffHappen();
</script>

The delicate part is to define what is “fast”, because there may be different reasons why the timing code runs slow. You could end up with too many false negatives or false positives.

EDIT: Updated JS example, as proposed by @fero. Thanks!

lxg
  • 12,375
  • 12
  • 51
  • 73
  • 2
    I think it might be better to use an inverted approach and create `div` elements for let's say 100ms and then count how many `div`s the browser created. Otherwise you may end up having a performance test that lasts too long - keep in mind that performing a time-consuming test operation on a slower client also increases the page load time which might already be longer than usual just because it _is_ a slower client. – fero Sep 08 '14 at 11:54
  • That's a good point, but it is much more complex to implement. You would have to create a background loop and kill it after a certain time, which is quite hard to do with JS. (You could also use the Performance API, but only on a few browsers.) Or am I missing something? – lxg Sep 08 '14 at 14:16
  • 2
    Yes, I think you're missing something. :-) You could just do it like `var start = new Date(), counter = 0; while (new Date() - start < 100) { createStuff(); counter++ }` and then look at `counter` to measure performance. – fero Sep 09 '14 at 05:49
  • Of course! I got carried away with some crazy asyncronous `setTimout` stuff while thinking of a solution. ;) Thank you very much! – lxg Sep 09 '14 at 06:54
  • Thx allot to both of you for some great input. I made my own version of your suggestions and implemented it and it works like a charm. :) – Some one else Sep 09 '14 at 07:15
-3

there is no direct way to find if page opened in desktop terminal, but there is a tricky way.. you can always find which mobile device user views, You can do it in javascript as well as php, Using that if any of the mobile device doesn't detected, then u can assume it is desktop, I have posted example code in php and js

PHP:

function is_mobile()  {
  if (preg_match("/Mobile|Android|BlackBerry|iPhone|Windows Phone/", $_SERVER['HTTP_USER_AGENT'])) {
    return true;
   }
}

if(is_Mobile){
        // Mobile!
    } else {
        // Not mobile
    }

JS:

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

if(isMobile.any()){
    // Mobile!
} else {
    // Not mobile
}

Hope this helps you all who want to detect if user opens page in desktop

Khaleel
  • 1,212
  • 2
  • 16
  • 34
  • Sorry, it does not help at all. The question is not about detecting desktop/mobile devices. – Salman A Sep 08 '14 at 10:45
  • @SalmanA I use the same to find the view port desktop or mobile. It worked for me like charm . How it doesn't help you at all – Khaleel Sep 08 '14 at 10:47
  • Please read the question again. OP wants to know if the browser accessing the website is running inside, for example, remote desktop session or teamviewer. – Salman A Sep 08 '14 at 11:02