Detecting Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Detecting Javascript

Post by onion2k »

What's a good method for detecting whether or not Javascript is switched on? I can think of a couple of methods (like putting <noscript><input type="hidden" name="noscript" value="1"></noscript>" in a form) but I was wondering what other options there are. Sending the user to a page that detects javascript and then uses a meta refresh to forward the user to a different page seems to be the 'recommended' approach. It seems pretty nasty to me though..

I know the best method is to write the site in a way that makes it unnecessary and then use JS to extend it .. but I'm working on a site that requires it.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You could use a reverse approach :)

In no_js_enabled.html

Code: Select all

window.top.location.replace("/with_js_enabled.html")
But I can't advice you what would be best ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I've decided to use a lazy, nasty hack .. putting a meta refresh tag inside a <noscript> block. If the user doesn't have scripting enabled they're shuffled off to the 'you got no Javascript' page. It's horrid but it works.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

I use:

<noscript>
<ing src="check.php" />
</noscript>

Inside my login forms...from that point on I just keep the value in a session...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

onion2k: I've seen that method being used before. I find it very, very annoying, but that's just the fact that the site needs JS speaking :-).

If you don't want to use a bouncer page, it might be a good idea to make pages "disabled" by default, and then have JavaScript enable them. This incurs, however, a performance penalty if you have many pages.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

As an added reason not to require javascript, spiders won't index your site properly.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I don't care about spidering. This is for something that won't be available to the public anyway. I just don't want the users moaning that my code doesn't work so I'm looking at ways to detect potential problems.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I would use the Mozilla Prism browser for web applications. This eliminates many of the problems which I have with JS compatibility, browser specific issues, etc. Also, it eliminates the Back/Refresh problem with AJAX applications - these functions simply don't exist in this browser (i.e. as buttons) :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

While I agree with the spirit of the previous suggestion, I'd go with XULrunner
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

I run a counter cookie at my site (used to trigger secrets on the last version and some of the retained secrets and the ones I have to add back...when they hit a certain number).

You could simply use the noscript element with an image though only have that noscript element present when the visitor's counter cookie equals a low value such as 2. Besides those who do not support cookies (bunker building apologetical privacy advocates) you'd get a unique and highly accurate idea of how many people have JavaScript disabled by the number of times the image was loaded in your access log for the month. This could be skewered by those who reset their cookie though in general unless you setup some sophisticated serverside IP tracker that reads your log and itself serves the image (since PHP is nifty and can do so) this makes the most sense to me.

I know AWStats tracks this by leaving the data in a requested URL that it later reads when compiling the log for you though I'm not exactly sure how it does it nor have I figured out how to have it show me those particular stats.
Post Reply