Javascript/CSS detection in PHP

Posted on 2003-10-26
Last Modified: 2013-11-19
I'm fairly certain that javascript detection isnt directly possible in PHP. However, I need some way of determining if they have JS enabled and if their browser is CSS capable. Any suggestions?
Question by:jmfreema
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Assisted Solution

errows earned 75 total points
ID: 9623341
For CSS:


  1.  Copy the coding into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the BODY of your HTML document  -->


<div id="checkcss" style="position:absolute;"></div>

<!-- Original:  Ronnie T. Moore, Editor -->
<!-- Web Site:  The JavaScript Source -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! -->

<!-- Begin
var yes = "yes-css.html"; // CSS page
var no  = "no-css.html";  // Non-CSS page

// How should we look for the (CSS) DIV ?
// Netscape will look with:  document.checkcss
// Internet Explorer will look with:  checkcss

checktype = document.layers ? document.checkcss : checkcss;

window.location = (checktype) ? yes : no;
//  End -->

For JavaScript:



<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! -->

<!-- Begin
jsver = "1.0";
// End -->
<SCRIPT LANGUAGE="JavaScript1.1">
<!-- Begin
jsver = "1.1";
// End -->
<SCRIPT Language="JavaScript1.2">
<!-- Begin
jsver = "1.2";
// End -->
<SCRIPT Language="JavaScript1.3">
<!-- Begin
jsver = "1.3";
// End -->

<B>Your browser does not currently support JavaScript.</B>
<P>If your are using Netscape 2 or later then you can enable JavaScript.
<P>Version 2 or 3: Choose Options|Network Preferences, choose the Languages tab, click Enable Javascript and then click OK.
<P>Version 4: Choose Edit|Preferences|Advanced, click Enable JavaScript and then click OK.

With Internet Explorer 5:  Go to Tools Menu|Internet Options, Security Tab  and click on the Custom Level. Then select disable active scripting under the scripting section.


<!-- Begin
document.write("<B>Your browser supports JavaScript version " + jsver + ".</B>")
// End -->

That's not done in PHP though...
you should take all info on the first page and redirect your user accordingly...


Assisted Solution

frankenstrat earned 75 total points
ID: 9623347
That is fairly straight forward. If a browser is javascript enabled, then you can use javascript  to forward the browser to page with javascripts.

<body onload="location.href='javascriptpage.html'">

I would say that it is extremely rare for a user to have javascript enabled and css disabled and therefore not really worth the time and effort to code a site for those six people.

Once you have determined that the user's browser is javascript enabled, you can use the following PHP script to present the appropriate stylesheet;


function inAgent($agent) {
   global $HTTP_USER_AGENT;
   $notAgent = strpos($HTTP_USER_AGENT,$agent) === false;
   return !$notAgent;

*  $browser will contain one of the following values:
*      'iewin' : IE 4+ for Windows
*      'iemac' : IE 4 for Macintosh
*     'ie5mac' : IE 5 Macintosh
*      'nswin' : Netscape 4.x Windows
*     'nsunix' : Netscape 4.x Unix
*      'nsmac' : Netscape 4.x Mac
*        'ns6' : Netscape 6 / Mozilla

if ( inAgent('MSIE 4') or inAgent('MSIE 5') or inAgent('MSIE 6') ) {
 if ( inAgent('Mac') )
   $browser = inAgent('MSIE 5') ? 'ie5mac' : 'ie4mac';
 elseif ( inAgent('Win') ) $browser = 'iewin';
} elseif ( !inAgent('MSIE') ) {
 if ( inAgent('Mozilla/5') or inAgent('Mozilla/6') ) {
   $browser = 'ns6';  
 } elseif ( inAgent('Mozilla/4') ) {
   if ( inAgent('Mac') ) $browser = 'nsmac';
   elseif ( inAgent('Win') ) $browser = 'nswin';
   else $browser = 'nsunix';
} else $browser = "unknown";

switch ($browser) {
      case iewin:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      case ie4mac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      case ie5mac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      case nswin:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      case nsmac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      case nsunix:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      case ns6:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      case unknown:
      print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=test\">\n";

Hope this helps

Author Comment

ID: 9624865
Those are both helpful, but is there any way to have it detect without forcing it to jump to another page? Like, for example, they go to my page and it loads index.php. Is it possible for index.php to know if javascript is enabled or not without forcing it to jump to index_js.php or index_nojs.php for example?
LVL 11

Accepted Solution

shmert earned 75 total points
ID: 9624997
Yes, this is possible.  Take a look at the get_browser() function:

It returns an object which contains lots of useful information about the browser which requested the current page.  Note that for this to work you need to download the browsecap.ini file and place it in the correct location.  This defines the feature sets of all the different browsers out there.

One problem is, I don't see any sort of CSS support in the list of keys for the browser object.  So if you want to include browser-specific stylesheets, you'll need to do a little research.  But the get_browser() function should help you to identify the family of browser very reliably.

Author Comment

ID: 9625060
Thank you all so much. That was very helpful.

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
can i read my emails on lamp ftp 4 70
Wordpress -  Updating All Posts on Website 3 93
Google Recaptcha 13 52
Developers / Staff Setup 10 43
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question