Hello guys,

is there a way to detect whether JS is enabled or not, using PHP codes?

Thanks in advance. =)

Well I would recommend an advanced system of ajax for best confirmation but if you want to keep it simple the following script will do the job.

$v=get_browser(null,true);
$javascript=$v['javascript'];
unset($v);
if ($javascript) {
    echo 'Javascript is enabled';
    } else {
    echo 'Javascript is disabled';
    }

Well I would recommend an advanced system of ajax for best confirmation but if you want to keep it simple the following script will do the job.

$v=get_browser(null,true);
$javascript=$v['javascript'];
unset($v);
if ($javascript) {
    echo 'Javascript is enabled';
    } else {
    echo 'Javascript is disabled';
    }

get_browser() only returns if the browser is capable, not if its enabled.

Here are many examples of how it can be done.

http://www.google.com/#hl=en&source=hp&q=check+if+javascript+is+enabled+php&aq=f&aqi=&oq=&fp=6d02e072335ea48a

There is no good way of doing it. The best thing is just to use the <noscript> tags and let them know that they need javascript enabled in order to view your site properly.

I never said that get_browser was the best option. I would suggest using ajax and below is a sample code.

<? if ($_GET['js']!==1 && $_GET['js']!=='1') {
/*include javascript in these headers.
  however do not place any more after the body
  tag in this section. Contents of the body tag
  go after the php if statement below this text.*/
echo '
<html><head><title>Webpage Title</title></head>
<body id="bodyid" name="bodyid">
<script language="javascript" type="text/javascript">
<!-- 
var ajaxRequest;
try{
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
        //do nothing
        }
    }
}
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        document.getElementById("bodyid").innerHTML = ajaxRequest.responseText;
        }
    }
ajaxRequest.open("GET", "test.php?js=1", true);
ajaxRequest.send(null); 
//-->
</script>
'; }
//above: initial header before body tag.

if ($_GET['js']==1 || $_GET['js']=='1') {
    echo 'this is the body of the page with javascript enabled.';
    } else {
    echo 'this is the body of the page with javascript disabled.';
    }
?>

But in that javascript be sure to replace the string test.php with the name of the file you saved as.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.