views:

51

answers:

2

Is there a better way to collect data on how many visitors don't have js enabled. My idea was to add a hidden value at page load with js. If it works then js was successful. But looks like there's no way to actually read it back to know if it was successful unless I do some kind of a page reload, but now it gets complicated (I have to delay whatever operations that were about to happen, etc. so as I said gets complicated). Any tips or ideas on this? I'm sure there's a better practice way than mine.

I should add, if there's already a ready-made solution for this, please let me know, I'm not really interested in reinventing the wheel :)

+5  A: 

A good way to do this is use a <noscript><img src="track.php" width="1" height="1" /></noscript>, and that will allow for browsers without javascript to pull a tracking image and then the server can get the Useragent and IP from that tracking image.

CodeJoust
I have done this before in a production environment and it works very well.
cjstehno
Even if you do this, there could be a delay in the request, or even it might get lost in the net - you can't rely on this to make server-side decisions...
Seb
Are there any problems with this approach? It reminds me of how cross-site scripting attacks work - mind you I don't know much about xss though.
Chris
They're not for server-side decisions, just for simple data collection actually. It should still work fine either way.
Chris
XSS shouldn't be a problem with this. It's the same as including a logo image, and the browser send out information to collect the image. What's different, though, is the image is only fetched if javascript is turned off.
CodeJoust
Letting anyone include an image tag means they can point it to their own server and gather data about users on your site. But in this case, it's not a security risk as you're the one providing the image.
mahemoff
+1  A: 

You can't know in advance which technologies the user is using client-side, so the only way to know for sure is after the first load. Even so, he might disable JS after the first page load and you're left running with a different scenario.

In fact, try it here in SO: load a page with JS enabled, then disable and reload. You'll see a big red banner at the top telling you this page works better with JS enabled.

Bottom line: you should never rely on client's technology, unless you really want to limit the people reaching your site. If you want to reach the most number of people, you should code as if they had every technology, and none at the same time.

Seb
Thanks Seb, I understand your point, though this was a strictly technical question. +1
Chris