Jump to content

Javascript --> PHP


Jonas

Recommended Posts

Ok, so I have an if condition in javascript that I wanted to convert to php, and I was wondering if this was correct:

  <script type="text/javascript">  	var useragent = navigator.userAgent;  	var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;  	if (bName == "Opera") {    document.write('<'+'link type="text/css" rel="stylesheet" href="opera.css" />')  	}  	else if (bName == "Netscape") {    document.write('<'+'link type="text/css" rel="stylesheet" href="netscape.css" />')  	}  	else {    document.write('<'+'link type="text/css" rel="stylesheet" href="microsoft.css" />')  	}  </script>

-->

  <?php  	$useragent = navigator.userAgent;  	$bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;  	switch (bName == )  	{    Case "Opera":    	echo('<link type="text/css" rel="stylesheet" href="opera.css" />');    	break;    Case "Netscape":    	echo('<link type="text/css" rel="stylesheet" href="netscape.css" />');    	break;    Default:    	echo('<link type="text/css" rel="stylesheet" href="microsoft.css" />');  	}  ?>

Is that correct? Does else if... work in php? It didn't look like it in the w3schools tutorial, so I tried with switch. Is Switch Case 1... Case 2... Default... the same as If... else if... else...

Link to comment
Share on other sites

PHP doesn't understand "navigator.userAgent". After a quick search on google, I found a browser detection script for php here.Other than that, I havn't seen "switch(variable ==)" before, but after thinking about it... it would make a lot of sense, so there's a good chance that it's legal and I just don't know about it. But! If your script isn't working, you might try "switch(variable)" instead... just in case.Also, for further PHP syntax questions... php.net has a great PHP manual that covers anything and everything about php.

Link to comment
Share on other sites

The switch command has to be like this:switch (variable){case "value1": ... ...break;case "value2": ... ...break;}And navigator.userAgent belongs to Html DOM, which cannot be used by php :(Why bother to create browser dependant stylesheets anyway :) Try to make one, independant of which browser you're using :):D

Link to comment
Share on other sites

Believe me, I tried that, and managed up until one point, when everything got screwed up. It's easier just using a script. The javascript works just fine, it's just that it gets caught as a a warning in the validator... :)And since php outputs finished html before the validator has a go at it, I thought it would be better...

Link to comment
Share on other sites

Well, I understand where you're coming from, but believe me, no can do :)Like that, that is. JavaScript can't just be translated, most of the time. When it is plain JavaScript without any DOM, it would be different storyYou could try to give the user the option to choose their selves, would that do it? If so, I'll explain how it can.

Link to comment
Share on other sites

You could try to give the user the option to choose their selves, would that do it? If so, I'll explain how it can.
Thanks, but I already had that once... :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...