Forum Moderators: coopster

Message Too Old, No Replies

Converting from Javascript to php - two questions

1. using 'document.layers'. 2. inserting javascript within php

         

HarryM

1:29 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have two questions re converting from Javascript to php.

1. Testing for NS4 browsers is easy with Javascript using "if (document.layers) {...whatever...}". Is it possible to use the same test in php?

2. Is it possible to include Javascript within a php include?

For example:

<?php require("../foobar.inc");?>

where foobar.inc is:

xhtml and text...
<script type="text/javascript">
if (document.layers)
{document.write("HI THERE");}
</script>
more xhtml and text...

Can this be made to work?

Any help appreciated

Harry

jatar_k

4:22 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



1. sure, take a look at $_SERVER [ca.php.net] superglobal array and particularily HTTP_USER_AGENT or also get_browser() [ca.php.net]

2. You include js with the script src="something.js" just like always. Keep in mind that php is run on the server side before the page is served. PHP builds and serves a regular old html document. There isn't always a need to convert your js. If your js works well why change it?

Best tool for each job, just because I use php doesn't mean I stop using html, css, js etc. I just have one more tool at my disposal which happens to be much more powerful than those others I listed. ;)

HarryM

5:19 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jatar_k,

I already use HTTP_USER_AGENT to give me browser info, but unfortunately it doesn't identify NS4 browsers as well as the javascript 'if (documents.layers)' test.

I suspect I am going to have to use javascript, but as you suggest use php to link it to the page.

Harry

isitreal

11:57 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This will work to identify Netscape 4x in PHP, it has to be in that order since IE has the Mozilla/4 string in it, also Opera sometimes will. Once you have which browser you need, just test like this in PHP: if ( $browser == 'ns4' ){statement...}:

// simple browser detection in php

$whichBrowser = $_SERVER['HTTP_USER_AGENT'];

if (stristr($whichBrowser, "Opera"))
{
$browser = 'opera';
}

elseif (stristr($whichBrowser, "MSIE 4"))
{
$browser = 'msie4';
}

elseif (stristr($whichBrowser, "MSIE"))
{
$browser = 'msie5x;
}

elseif ((stristr($whichBrowser, "Konqueror")) ¦¦ (stristr($whichBrowser, "Safari")))
{
$browser = 'safari';
}

elseif (stristr($whichBrowser, "Gecko"))
{
$browser = 'gecko';
}

elseif (stristr($whichBrowser, "Mozilla/4"))
{
$browser = 'ns4';
}
else
{
$browser = 'unknown';
}

HarryM

12:48 am on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks isitreal,

That is the way I identify all the other browsers, but I had overlooked the point that by excluding the other contenders the only remaining Mozilla/4 had to be NS4.

The only point I'm concerned about is Mac browsers about which I know nothing. Are there any non-Windows browsers defined as Mozilla/4?

Although I suppose I could cover that by also testing for the presence of "Win".

Harry

isitreal

6:52 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Testing for win or mac is the same:

if ( stristr($whichBrowser, "Mac") ){
$os = 'mac';
}
elseif ( stristr($whichBrowser, "Win") ){
$os = 'win';
}
else {
$os = 'other';
}

so if you want to specify browsers:

if ( ($browser == 'ie'){
if ( $os == 'mac' ){
$browser = 'iemac';
}
elseif ( $os == 'win' ){
$browser = 'iewin';
}
}

or something to that affect. I haven't seen any significant rendering differences on Mozilla between os's, although Opera mac is very different from opera windows, but there are virtually no opera macs out there so it's not really worth worrying about.