954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is there a way to detect Javascript using PHP

Hello guys,

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

Thanks in advance. =)

mvt6204
Newbie Poster
5 posts since Apr 2009
Reputation Points: 10
Solved Threads: 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';
    }
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

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 tags and let them know that they need javascript enabled in order to view your site properly.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

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.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Note that in php manual there is warning that although get_browser function is very comprehensive it has a sizeable overhead that may impact performance if you are using this for each load of the site:
http://us.php.net/manual/en/function.get-browser.php

CalmQuiet
Newbie Poster
1 post since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: