[Javascript] Javascript detection

Peter-Paul Koch pp.koch at gmail.com
Tue Jan 2 19:00:27 CST 2007


Nice idea, but in general I share the criticism that it's somewhat too
complicated.

If I want the server to know whether JavaScript is enabled, I always
use a hidden form field. I set its value to "no" in the HTML, and a
JavaScript (which can be extended by as many object detects as you
like) changes the value to "yes". The value of the hidden field is
sent back to the server, and JavaScript is decisively detected without
any possible error.


> David et al:
>
> To explain what I'm trying to do is probably too complicated for my
> limited communication skills.
>
> The original post that started me on this investigation was simply
> "How does one take the value of a javascript variable and carry it
> forward to the next page?" However, no one was able to provide an
> answer, other than using cookies and I don't want to do that.
> However, I believe that it can be done otherwise and that's what I am
> trying to accomplish.
>
> What I am doing is not essential to the operation of my web page and
> if javascript is not enabled, then the enhancement is not realized
> nor needed -- thus satisfying "progressive enhancement" requirement.
> So, we should not address that topic any further.
>
> My first step in this investigation is to determine if javascript is
> enabled in the user's browser -- the following is my methodology.
>
> 1. The user is directed to this url:  http://sperling.com/js_detect
>
> The "js_detect" is a folder containing [1] (index.php) the default
> file, the code is listed below. As you can see, this [1] (index.php):
> a) starts a php session [js]; b) loads a javascript named [2]
> (is_js.js); c) makes the current index.php page refresh to
> "index1.php" after one second; d) and note, the index.php page has no
> visible content -- thus, the user doesn't see anything.
>
> 2. Now, javascript [2] (is_js.js) simply executes "a.php" script
> after an onload event and passes the js variable to "a.php" via a
> javascript POST operation -- IF -- javascript is enabled.
>
> 3. The [3] (a.php) simply takes a javascript POST and sets a php
> session js variable. Actually the else portion of the code is never
> executed for this would never be called is javascript wasn't enabled.
> I only present it for clarification.
>
> 4. The [4] (index1.php) simply displays the php session variable js.
>
> Now, I don't fully understand why I have conflicting reports. Some
> people have the identical browser and OS combinations and report that
> it both works and doesn't work.
>
> And, I don't understand why browsers running on LINUX and Opera on
> Windows machines fool the script. A more complete accounting is here:
>
> http://www.browsercam.com/public.aspx?proj_id=311334
>
> In any event, the technique works for most browsers and OS's.
>
> Thanks for your review and comment.
>
> tedd
>
> --- code follows ---
>
> [1] --- index.php
> ------------------------------------------------------------------
>
> <?php session_start();
>
> if (!isset($_SESSION['js']))
> {
> $_SESSION['js'] = 0;
> }
> ?>
> <html>
> <head>
> <meta http-equiv="content-type" content="text/html;charset=UTF-8">
> <title>?</title>
> <script type='text/javascript' src="is_js.js"></script>
> <meta http-equiv="refresh" content="1;
> URL=http://sperling.com/js_detect/index1.php">
> </head>
> <body>
> </body>
> </html>
>
> --- end of index.php
> --------------------------------------------------------------------
>
> [2]--- is_js.js ----(Thanks to Matt for the idea given a week
> ago)---------------
>
> function is_js()
> {
> (new Image()).src = 'a.php?js=1';
> }
> window.onload = is_js;
>
> --- end of is_js.js
> --------------------------------------------------------------------
>
>
>
> [3] --- a.php
> -----------------------------------------------------------------------
>
> <?php session_start();
>
> if (isset($_GET['js']))
> {
> $js = 1;
> }
> else
> {
> $js = 0;
> }
> $_SESSION['js'] = $js;
> ?>
>
> --- end of a.php
> ---------------------------------------------------------------------
>
> [4] --- index1.php
> ------------------------------------------------------------------------
>
> <?php session_start();
> if (!isset($_SESSION['js']))
> {
> $_SESSION['js'] = 0;
> }
> ?>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">                 <html>
> <head>
> <meta http-equiv="content-type" content="text/html;charset=UTF-8">
> <title>Stuff by tedd</title>
> <link rel="stylesheet" href="style.css" media="screen">
> </head>
> <body>
> <p>
> Javascript has <?php if($js==0){ echo '<b>not</b>'; } ?> been
> detected on the client.
> </p>
> </body>
> </html>
>
> --- end of index1.php
> ---------------------------------------------------------------------
> --
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>


-- 
-------------------------------------------------------------------
ppk, freelance web developer
http://www.quirksmode.org/
------------------------------------------------------------------



More information about the Javascript mailing list