0

Hello guys,

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

Thanks in advance. =)

4
Contributors
4
Replies
7
Views
8 Years
Discussion Span
Last Post by CalmQuiet
0

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';
    }
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.