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>
.
I would use a cookie. It will function nearly identical --
ReplyDelete<?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!
A rather unique (and IMO lame) approach is to use javascript to download a fake image:
ReplyDelete<script type="text/javascript">
var fake = new Image();
fake.src = "/sess_js.php";
</script>
Doesn't use AJAX, jQuery, Cookies, etc.
First you need to learn to do a ajax call in pure javascript, some website like w3schools or others
ReplyDeleteAnd 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.
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.
ReplyDeleteCreate 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"