Forum Moderators: coopster

Message Too Old, No Replies

Using PHP and Javascript.

PHP and javascript

         

jlommori

6:15 pm on Jul 11, 2006 (gmt 0)

10+ Year Member



I want to detect what platform that the user is using then select the correct stylesheet for that platform.

I'm defining a variable and assigning the platform to it in javascript.

var OS = navigator.platform

then I want to call my stylesheet using PHP include. Like this:

if (OS=="MacPPC")
{
<?php include("/stylesheet1.css");?>;
}

However this statement does not work at all. Any suggestions? Or is there a way to call a file from Javascript?

StupidScript

6:27 pm on Jul 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome, jlommori!

You're going about it a little backwards ... :)

PHP is rendered at the server, and then the page is delivered to the client (browser) where Javascript can do it's thing.

You need to do one of two things:

1) Use Javascript for the whole thing, or
2) Use PHP for the whole thing

1)

if (OS=="MacPPC")

{

document.write("<style type='text/css' href='/stylesheet1.css' />");

}

2) To use PHP for OS detection, you will need to either parse out the OS reference from the

$_SERVER['HTTP_USER_AGENT']
string or incorporate something like the PEAR
Net_UserAgent_Detect
[pear.php.net] class, which is based on the Javascript functions used by the above scriptlet.

[edited by: StupidScript at 6:40 pm (utc) on July 11, 2006]

StupidScript

6:05 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm an idiot ... :)

if (OS=="MacPPC")

{

document.write("<link rel='stylesheet' type='text/css' href='/stylesheet1.css' />");

}

D'oh!