Solved

Javascript/CSS detection in PHP

Posted on 2003-10-26
5
1,091 Views
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?
0
Comment
Question by:jmfreema
5 Comments
 
LVL 2

Assisted Solution

by:errows
errows earned 75 total points
ID: 9623341
For CSS:

<!-- ONE STEP TO INSTALL CSS SUPPORT REDIRECT:

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

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

<BODY>

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

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

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- 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 -->
</script>


For JavaScript:

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

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

<NOSCRIPT>
<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.
<P><HR><P>

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.
</NOSCRIPT>

<BODY>

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


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

errows
0
 

Assisted Solution

by:frankenstrat
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;

<?php

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";
      break;
      case ie4mac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case ie5mac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case nswin:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case nsmac:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case nsunix:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case ns6:
      print "<LINK REL=\"StyleSheet\" HREF=\"test.css\" TYPE=\"text/css\">\n";
      break;
      case unknown:
      print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=test\">\n";
      break;
      }
?>

Hope this helps
frankenstrat
0
 

Author Comment

by:jmfreema
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?
0
 
LVL 11

Accepted Solution

by:
shmert earned 75 total points
ID: 9624997
Yes, this is possible.  Take a look at the get_browser() function: http://php.net/manual/en/function.get-browser.php

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.
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now