I want two different includes to be run based on JS's browser recognition. I want to use this:

<SCRIPT language="JavaScript">
<!--
var browserName=navigator.appName;
if (browserName=="Netscape")
{
println("<?php include_ONCE(text1.inc)?>");
}
else
{
if (browserName=="Microsoft Internet Explorer")
{
println("<?php include_ONCE(text2.inc)?>");
}
}
//-->
</SCRIPT>

Feedback?

    Won't work. You come from a JS background, I assume? PHP is server side. You might accomplish what you want by calling 'window.location("http://mysite.com/foo.php?inc=text1.inc")' or somesuch based on the ouput of the browserName variable, but I'm no Javascript expert.

    PHP browser recognition would be more like:

    if (stristr($_SERVER['HTTP_USER_AGENT'],"MSIE")) {
       echo "Why not switch to Firefox??";
    } else {
       echo "I see you've been enlightened!";
    }

    😃

    Try a test page that prints the value of the HTTP_USER_AGENT variable, and try it from different browsers; or, just check your server access logs. Apache, at least, lists this information with each logged request.

    HTH,

      In general, it's a really bad idea to send browser-specific code.

      1. Browsers sometimes have their user agent hidden
      2. Browsers sometimes lie about what they are
      3. You cannot possibly test your application in browsers which haven't yet been released - so your browser-detection code may cause them to break.
      4. You'll create more work for yourself maintaining and testing the browser-specific parts of your application - and keeping them in sync.

      You should REALLY try to avoid this if at all possible.

      Feature-detection is usually possible in Javascript. If you're trying to do browser-detection to cope with Javascript differences, do client-side feature-detection instead.

      Mark

        There are ways around that. You test for functionality, and you use if(window.opera) for opera. Even when cloaked, that will still work. AppNAme and even UserAgent string are not the best methods. For example if you want to be sure it's IE, use

        if(document.all)&&(!window.opera)

        Gecko doesn't support doc.all and the 2nd part of the compound if eliminates opera (which does support doc.all), so the only 1 that should pass that test is IE.

          Maybe it's just me being silly here but why don't you just use php instead of the javascript to decide the includes....

          <?

          if ($_SERVER['HTTP_USER_AGENT'] == 'yadayada')
          include("thisfile.inc");
          else
          include("thatfile.inc");

          ?>

            Write a Reply...