SUSE Conversations


How to check if Javascript is enabled on the client side using php



By: mendesdomnic

September 3, 2009 3:58 pm

Reads:3062

Comments:0

Rating:3.0

You might be using validations for your form on client side using Javascript. It may arise that client side Javascript is disabled, given this scenario your validations will go for a toss.

You can check in your php if Javascript is enabled on client side using the following method.

Add a hidden input in your form in the <noscript> tag. The contents of noscript tag are rendered on a browser with Javascript disabled:

<form id="" action="" method="post">
	<noscript>
		<input name="js_enabled" type="hidden" value="1">
	</noscript>
	<input id="email" name="email" size="30" class="required email" ><br>
	<input id="submit_data" value="Submit" type="submit"><br>
</form>

Now the following will be the php code to check whether Javacript is enabled:

<?php

// Check if Javascript is enabel from hidden value in <noscript></noscript>
$browser_check = 0;
if(isset($_POST['js_enabled']))
	$browser_check = $_POST['js_enabled'];

if ($browser_check == 1) {
	echo ' Javascript is disabled on your system';
}

?>

This is useful if you use ajax submission of form. Say to check if email exists and display the same in error div. Or maybe other scenarios that may suit your requirements.

VN:D [1.9.22_1171]
Rating: 3.0/5 (2 votes cast)
VN:D [1.9.22_1171]
Rating: -3 (from 3 votes)
How to check if Javascript is enabled on the client side using php, 3.0 out of 5 based on 2 ratings

Tags: , ,
Categories: Enterprise Linux, Technical Solutions

Disclaimer: As with everything else at SUSE Conversations, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.

Comment

RSS