1

Yes, I'm aware that this question has already been post, but ... I'm looking for a way to check if the Client has Javascript Enabled.

Here is my idea : At every loading page, PHP initialize $_SESSION['is_js'] to 0. Then, I wish AJAX to (via launching a script php ?) try to set $_SESSION['is_js'] to 1. If JS is enabled, AJAX will succeed, if not, the value remains 0.

I can't use jQuery or others libraries... I have to write this in pure AJAX (I mean not using framework or librairies), and I have no idea how to do this.

Edit : I can't use cookie. I don't want to use <noscript>.

4wk_
  • 2,458
  • 3
  • 34
  • 46
  • 2
    http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled - read that first and please stop misusing AJAX.. you can't "write something in PURE AJAX", that makes no sense at all. It's not a programming language, and mostly you don't even use X part of AJAX nowadays. – N.B. Feb 13 '12 at 16:39
  • Powerfull comment MetalFrog. @N.B. : well, I express myself very bad. I will edit. – 4wk_ Feb 13 '12 at 16:41
  • Also, if you can't use a cookie and don't **want** to use ` – N.B. Feb 13 '12 at 16:42
  • I don't want to use `` cause I need the information (is JS enabled, checked at every loading) in every page. – 4wk_ Feb 13 '12 at 16:46
  • Build using [progressive enhancement](http://en.wikipedia.org/wiki/Progressive_enhancement) and you won't need to check that any more. – Quentin Feb 13 '12 at 16:51
  • This is starting to sound like a homework assignment. Why are you so restricted on all the common, useful tools? – Mikhail Feb 13 '12 at 16:51
  • AJAX = "asynchronous Javascript and XML". What you want is Javascript + PHP. In addition, if you cannot use cookies then the only thing left is that nasty old GET params passing. – Xeoncross Feb 13 '12 at 16:51
  • Can you explain why you can't use a cookie? Using a cookie is a much better solution than using ajax so perhaps it's better to find a way of overcoming the reason why you can't use cookies. – wheresrhys Feb 13 '12 at 16:51
  • Mikhail : Sometimes, for a particular client, you just CANT what you want ... Xeoncross : some guys tell me that JS (without AJAX) can't interact at all with PHP. – 4wk_ Feb 13 '12 at 16:57
  • Quite odd that the client is instructing the expert on how to implement something, isn't it? I'm not telling my car mechanic **how** to fix my car, I pay him to do it. – N.B. Feb 13 '12 at 17:04
  • Believe me, I will not stand this one for long -- – 4wk_ Feb 13 '12 at 17:08

4 Answers4

1

First you need to learn to do a ajax call in pure javascript, some website like w3schools or others

And in the php files that the ajax call , you can set the session variable to 1.

But there's maybe better way to find if the client have javascript enable.

EDIT: I suggest to check if your session variable is , if not, try the ajax call and set the variable at 0 . If at page loading the variable is set and equal, that means the client doesn't have javascript/xmlhttprequest enable.

And for a session, a cookie is store on the client side. So if the client refuse to store any cookie, you will never haver information in your session variables.

Nettogrof
  • 2,116
  • 2
  • 15
  • 22
  • I looked for any better way for that, but I didn't. Let me know if you found something better ! – 4wk_ Feb 13 '12 at 16:37
  • @Ash_ Oh your question is : you want to find the best way to check if client have javascript enable ? – Nettogrof Feb 13 '12 at 16:43
  • Yes but I would specify "at every loading page" : I want to have this information in PHP all the time (except first connection, of course) – 4wk_ Feb 13 '12 at 16:48
0

I would use a cookie. It will function nearly identical --

<?php
$_SESSION['js'] = (int) $_COOKIE['js']
setcookie('js', 0);
?>
<script type="text/javascript">
setCookie('js', 1);
</script>

You can use a set of cookie functions such as http://www.w3schools.com/js/js_cookies.asp

Good luck!

Mikhail
  • 8,692
  • 8
  • 56
  • 82
  • 1
    PHP function is `setcookie()`, not `set_cookie()`. Also, quoting w3schools as a relevant source is a big no, that site is so full of misinformation that it's not worth reading anything there. Other than that, the idea is good. – N.B. Feb 13 '12 at 16:40
  • 2
    @Ash_, if you can't use cookies then SESSIONS won't work -- they rely on cookies – Mikhail Feb 13 '12 at 16:45
  • Well, of course I use cookies throw $_SESSION, but I don't want to use $_COOKIE because data is transmitted in HTTP header. (that is not the case of $_SESSION) – 4wk_ Feb 13 '12 at 17:00
0

A rather unique (and IMO lame) approach is to use javascript to download a fake image:

<script type="text/javascript">
var fake = new Image();
fake.src = "/sess_js.php";
</script>

Doesn't use AJAX, jQuery, Cookies, etc.

Mikhail
  • 8,692
  • 8
  • 56
  • 82
0

According to your idea and your requirements, if you can't use a library like jQuery or Prototype, and so on... you are enforced to use the "raw" XMLHttpRequest Object. The following code is an example.

Create a file called ajax_js_enabled_test.php and put this code inside:

<?php

    if(isset($_GET['PHPSESSID'])) session_id($_GET['PHPSESSID']);
    session_start();

    if(isset($_SESSION['user_js_support']) && $_SESSION['user_js_support']) echo "Javascript enabled";
    else $_SESSION['user_js_support'] = FALSE;

?>
<script type="text/javascript">
    var XHR;
    if (window.XMLHttpRequest){
        // The code for IE7+, Firefox, Chrome, Opera, Safari
        XHR=new XMLHttpRequest();
    }else{
        // The code for IE6, IE5
        XHR=new ActiveXObject("Microsoft.XMLHTTP");
    }
    XHR.open("GET","http://127.0.0.1/check_js_user_support.php<?php echo '?PHPSESSID='.session_id(); ?>",true);
    XHR.send();
</script>
<p>
    <a href="<?php echo '?PHPSESSID='.session_id(); ?>">
        Refresh
    </a>
</p>

Create another file called check_js_user_support.php and put this code inside:

<?php

    if(isset($_GET['PHPSESSID'])) session_id($_GET['PHPSESSID']);
    session_start();

    $_SESSION['user_js_support'] = TRUE;

?>

Copy both files in your local web server root. Open the page ajax_js_enabled_test.php from your browser. The AJAX request starts and the page check_js_user_support.php will set the session param called user_js_support to TRUE. So by pushing the Refresh button the page will be reloaded, the $_SESSION['user_js_support'] will be TRUE and the script will write: "Javascript enabled"

bitfox
  • 2,281
  • 1
  • 18
  • 17