FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Code that only displays in IE!





Michael B
A little tutorial that some might find very use full.

Every got a page set up so it look prefect and then you have a look at it in Internet Explorer (here after know as IE) and it looks terrible? And then thought if only I could add some code that only shows up in IE. Well, you can!!

The first thing you need to do is test of your server supports php, this is easy. (frihost dose so you could skip this part.)
Code:
<?php phpinfo(); ?>

Copy that into your text editor and save it as info.php upload if to your server and open it. If it works you will get a page that tells you all about the the php set up on your server, if not you get a blank page.

Well now we have to turn your pages into php, that is easy just change the end from .html or .htm to .php
Code:
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
The Text that you want anyone using IE to see, this can include HTML.
<?php
}
?>

Ok great so now we have code that the server only sends out to the user if they are useing IE (php is server side programing, what gets sent to the user is HTML). If you look into thing a bit more you will soon see you have created another problem! Opera users see the text too! So we have fix that, here is the code
Code:
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE) {
?>
This is were you put code that only Opera uses see, leave it black so they don't see anything that we only what to show up in IE.
<?php
} else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
Ok so this is were you put code that only IE users see and no one else.
<?php
}
?>

Fine you say, my problem is not the HTML code but my css.

Well you have two options. First add a some css directly into the head of the page like this
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Title</title>
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE) {
?>
Again we have to stop it working in Opera as well, so we leave here blank
<?php
} else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
<style>
Your css code for IE only
</style>
<?php
}
?>
</head>

The other option is to have a css file for IE and another css file for every one else. Like this
Code:
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE) {
?>
<link rel="stylesheet" type="text/css" href="default.css" title="Default"> <-- again we have to give Opera something first, you will want to make this your default css file.
<?php
} else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
?>
<link rel="stylesheet" type="text/css" href="ie.css" title="Internet Explorer"> <-- this is the IE only css file.
<?php
} else {
?>
<link rel="stylesheet" type="text/css" href="default.css" title="Default"> <-- This is for every other browser the works as it should.
<?php
}
?>

Ok find but now you want to do it to browsers other than IE. If you looked at the code you might have noted that for Opera there is 'Opera' in the code and for Internet Explorer 'MSIE' well that is what you have to change here is a table of posible options (there might be others but these are the most common).
Code:
MSIE = Internet Explorer
Opera = Opera
Gecko = Mosilla Suite / SeaMonkey
Firefox = FireFox
Netscape = Netscape
Konqueror = Konqueror
Phoenix = Mozilla Phoenix
Firebird = Mozilla Firebird
Safari = Safari


Have fun!

Oh and did I tell you that
Code:
<!--[if IE]>The text that only Internet Explorer sees<![endif]-->
works just as well, you just can get the fanny effect with other browsers. :twisted: (I just had to leave that till last)
riv
What do you mean by the fanny effect?
sprinqles
Thanks for posting that Michael B. Very very helpful tips and info you got there. I myself don't only use IE to browse websites because I get annoying pop-ups when I use IE.
Michael B
Sorry riv that was a typo (why dose every one have to notice all my typos) I sould be fancy, I think :P

I know what you mean, on my site since it is about FireFox extentions I have a message that shows only in IE saying to get FireFox, not that I reomend that for most sites.

There is a lot of other great thinks you can do with PHP like compress you html code, my favorite :)

Or have ?color=red to change the theme of your site. So users that don't like the default can change it (and spend lots of time on you site testing out the different themes :) )

Michael
site.frih.net
engeland
this is a useful one. Been lookin around for something similar.

Thanx Wink
n0obie4life
http://www.javascriptkit.com/howto/cc2.shtml

I prefer the "if IE" comment tag Wink
Michael B
I do too n0obie4life, but I had to lead every one through a long tutorial before sticking the

Code:
<!--[if IE]>The text that only Internet Explorer sees<![endif]-->


at the end, it is not fun other wise :twisted:

I send my CSS and JavaScript through the php processer as will, so I can use it there, normaly I only do it for correcting CSS things for IE and, well what every you call that silly mac web browser.
Marston
Browser dependant css simplified:

Code:
attribute-for-good-browsers: 100px;
*attribute-for-good-browsers: 100px; /* only displays in IE. */
Related topics
Reply to topic    Frihost Forum Index -> Webmaster and Internet -> Design Tips

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.