3

I never thought that I am stupid, but... I found hundreds of ways to create if statement in html. Some says that right way is:

<!--[if IE]>
   You're using IE!
<![endif]-->
<![if !IE]>
   You're using something else!
<![endif]>

Or this:

<![if !IE]>
//code
<![endif]>

Or look this fiddle: http://jsfiddle.net/APFZh/2/

All of this DON'T works on my pc. Why? All says that "you have to do this..." and such answers are suggested, so they have to contain working examples, but...

Why they don't work in my browsers? What is the correct way to write conditional statements? Is example from fiddle is working?

enter image description here

I tried to open fiddle in ie 11, not with FF and changed useragent. Epic.

Why it is so ? What I have to do? Help me please!

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87

2 Answers2

7

Conditional comments were disabled in IE10 so they will not work in IE11.

Source

In order to have conditional statements you can use Javascript to detect the browser. Since I see Mootools in your screenshot, here is a guide on how to do that.

Or, here is a JS library that is pretty good: WhichBrowser

That being said, browser sniffing is not recommended. Look into Feature Detection instead.

Dryden Long
  • 10,072
  • 2
  • 35
  • 47
  • Okay. I thought it is not simple task, isn't it? I mean detecting user agent with JS. Is there a way to do this without external library? Also, why conditional not works in ff? – Sharikov Vladislav Apr 16 '14 at 20:31
  • So, I have to use WhichBrowser to detect browser and use for example [this](http://stackoverflow.com/a/577002/2898694) method to load css, right? – Sharikov Vladislav Apr 16 '14 at 20:49
  • @SharikovVladislav That's one way of doing it, yes. – Dryden Long Apr 16 '14 at 20:49
  • Mhm. There are no link to download `detect.js`. https://github.com/NielsLeenheer/WhichBrowser Readme says, that I have to upload detect.js to dir in my site and make reference in my html code. – Sharikov Vladislav Apr 16 '14 at 20:59
  • That's probably a question for the developer then. I've never used WhichBrowser before so I have no idea about it's dependencies... – Dryden Long Apr 16 '14 at 21:02
  • Okay. I will better download and user mootols then. +88 kb :( Nice chance to add tooltips :) – Sharikov Vladislav Apr 16 '14 at 21:09
  • ARgh!!! But I need to play with some elements in code. How I can do this easily with JS :'( HTML conditionals were the solution... =/ – Sharikov Vladislav Apr 16 '14 at 21:10
  • @SharikovVladislav Google "Javascript browser detection" and you should find plenty of tutorials. If you get stuck with one of them, come back and post a new question with the code you are having problems with. – Dryden Long Apr 16 '14 at 21:11
  • Nono. I know how to detect browser now :) Mootools provides good object for it. I meant how to manage elements on page, using js. – Sharikov Vladislav Apr 16 '14 at 21:14
  • I don't know what you mean by that. It seems like a broad question. You may want to start a new question since this one is starting to get off-topic. – Dryden Long Apr 16 '14 at 21:16
  • Solution for what I meant: using css class with display: none for example; another solution: use JS to remove/add items to the dom. – Sharikov Vladislav Dec 06 '16 at 19:11
2

You are not closing the comment tags correctly - it should look like this

<!--[if IE]>
   You're using IE!
<![endif]-->

<!--[if !IE]>
   You're using something else!
<![endif]-->

For extensive information on Conditional Statements see - http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx

AnuragBabaresco
  • 604
  • 8
  • 19