views:

332

answers:

6

Can I use specific html if the browser is IE? (Assuming FF is the default browser)
Foe example:

html line 1  

if IE 
html line 2  
else
html line 2  

html line 3    
html lin3 4

I am aware of using different CSS, but that won't work for this.

Thanks.

A: 

One way you can do this is using Javascript to check which browser is being used. You could have default HTML in your file and then hava Javascript change it out, or add to it, if the user is using IE.

Peter
+8  A: 

Sure - conditional comments

annakata
+6  A: 

IE understands "conditional comments," which you can use to selectively show markup even to specific versions. Here's an introduction:

http://www.quirksmode.org/css/condcom.html

Ash Wilson
+1 quirksmode is always the better link
annakata
+2  A: 

Can you show us the actual markup you're wrestling with? There may be solutions that don't involve browser sniffing.

DDaviesBrackett
Ok, posted here: http://stackoverflow.com/questions/1088922/how-to-get-a-usageprogress-bar-to-display-properly-in-ie
Tommy
+1  A: 

You could use conditional comments for code only IE reads:

<!--[if IE]>
IE only code here.
<![endif]-->

Or PHP:

<?php
   function ae_detect_ie()
   {
     if (isset($_SERVER['HTTP_USER_AGENT']) && 
     (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
        return true;
          else
         return false;
    }
  ?>

<?php  if (ae_detect_ie()) { code here } else { code here }  ?>

Ryan

Ryan
+1  A: 

I'd like to offer a bit more explanation, since a solution to your question necessarily involves both "downlevel-revealed" and "downlevel-hidden" conditional comments. Both work very well in Internet Explorer, but it's in non-Microsoft browsers that the distinction becomes important:

Content inside "Downlevel-revealed" conditional comments will always display in non-Microsoft browsers (since they do not follow the standard <!-- --> syntax of HTML comments).

Content inside "Downlevel-hidden" conditional comments (which seem to be discussed more often) will never show up in other browsers (since they do follow the standard <!-- --> syntax of HTML comments).

So, marking up your example code:

html line 1

<!--[if IE]>
html line 2
<![endif]-->
<![if !IE]>
html line 2  
<![endif]>

html line 3
html lin3 4