PDA

View Full Version : Detecting if JScript enabled ?


Nomadicus
08-15-2002, 11:57 PM
I use PHP for most of my server side progamming. But I also like JScripting for a lot of client side stuff.

Is there any way that JS can tell if JS has been disabled in the user's browser. (PHP can't handle this.) I know this sounds like a Catch-22, but I need to know this for form validation.

If the user has JS disabled, then I want to send them on to my second level of form validation using PHP.

TNX in advance.

mordred
08-16-2002, 12:16 AM
That's indeed tricky, it falls in the same category of writing a program that detects whether you computer is running or not - and that shall on exactly this computer. ;)

Seriously, you can't tell by JavaScript if it has been disabled. Just not possible. What you can do is make use of the <noscript> tag. Browser should render it's contents if they have scripting disabled, so you could do

<noscript>
<meta http-equiv="refresh" content="0; URL=http://mydomain.com/nextlevel.php" />
</noscript>

and that should automatically send the user to the next page. Alternatively, provide them only the parts that need JS by the help of JS, i.e. setting a <div>'s visiblitiy to "visible" by help of JS.

Spookster
08-16-2002, 05:51 AM
<script language="Javascript">
jsEnabled = true;


if(jsEnabled)
alert('Javascript Enabled Browser');
</script>

but as mordred says using noscript tags is the best way to go. It is very unlikely thought that you will come across too many visitors that aren't javascript enabled.

Majority of people use IE and most of those people wouldn't know how to disable it.

brothercake
08-16-2002, 10:18 AM
Originally posted by Spookster
It is very unlikely thought that you will come across too many visitors that aren't javascript enabled.


Not at all; roughly 10% of all internet users have no javascript, and in most cases this is because of LAN firewalls stripping it out

Nomadicus
08-16-2002, 04:04 PM
I know a lot more about PHP than I do JS (but am learning). My reference book tells me this:

"The NOSCRIPT element provides alternative content for browsers unable to execute a [java]script."

So with your construction as:

<noscript>
<meta http-equiv="refresh" content="0; URL=http://mydomain.com/nextlevel.php" />
</noscript>

. . . if I understand you properly, this tag somehow alerts the browser (IE, NS, Opera, etc.) that if it can't run any jscripts, to do the following instead (the following being the code inside the tags)? So, in this case, nextlevel.php would be my PHP script that does the form validation? Is this correct?

Ok, so if the browser can run jscripts, then it ignores the code inside the tags and my jscript validations will get done, right?

TNX. Just want to be sure I got all that.

mordred
08-16-2002, 07:49 PM
Yeah, in the essence you're right about what this <meta> tag is supposed to do. Only that it does not "execute" the "code" inside it, it's just a prefined attribute value that tells the browser to load the URL defined in a time measured in seconds by the number before the URL.

All browsers I know of would work as described - except NN2, because he knows JS, but not the <noscript> tag, so he ignores the content within. But on the other side, some people might argue that NN2 is no *real* browser ;)

Only thing I'm wondering is why you want to redirect the user for form validation. I always use javascript validation in the form directly, and server-side afterwards on the receiving page. This way the user won't have the comfort of a pre-validation by JS and may be forced to send his (incorrect) data repeatedly to the server, but since you're accustomed with PHP, I'll surely tell nothing new to you.

Nomadicus
08-17-2002, 12:55 AM
I can do validations in my sleep with PHP. But it requires a round trip from the client to the server, and back again. So I am told that JS is much better suited for this validation task, as it is all client side (no server hits required).

So I decided to start learning some more JS to take the load off the server. Then I discovered that some people turn off JS! "Now what, genius?" I asked myself. So I thought up this two-tiered approach. Maybe it's not worth the trouble, dunno, and should just keep everything in PHP for validation purposes. With the modern day CPU speeds and modems and all that, it may not really matter.

I remember years ago when every byte was precious and the tweek geeks would spend days "reducing" their code. But does this really matter anymore in terms of validation hits against the server?

joh6nn
08-17-2002, 05:35 AM
your best bet is to always validate on both the client and server side. that information has to be sent to the server anyway, so you should validate it there, as that's where you'll be using , especially if you can't always be sure that javascript is turned on. at the same time, including javascript validation is more userfriendly, because users to have to wait for the form to submit, and the next page to load, to find out that they made a mistake. so it's always a good idea to just do both.

Red_Punisher
09-10-2002, 09:02 PM
Is there any way on the client side to redirect an non JS enabled browser?

I understand the <noscript> trick, but I want to display a message if the browser tries to go to any page in the entire domain.