0

I have this snippet of code:

<script type="text/javascript">
$(window).load(function() {
    if(!$.browser.msie){ 
        $('#myDiv').animate({opacity: 1}, 300);
    } else if ($.browser.msie) {
        $('#myDiv').css({opacity:1});
    }
}); 
</script>

How can I say the same thing using the " : something ? somethingElse " syntax?

Thanks in advance?

4
  • 4
    Don't use ternary operators as a generic if/else. Use them for simple assignments, that is what they are designed for. Real if/else across more lines are more readable. Embrace readability over code golf.
    – Quentin
    Feb 12, 2013 at 14:57
  • 2
    The long and short of it is you probably don't want to. Nested ternary syntax gets very ugly. Feb 12, 2013 at 14:57
  • 2
    ... also the else if ($.browser.msie) { is redundant. A simple else { would be enough.
    – Yoshi
    Feb 12, 2013 at 14:57
  • I agree with you 100%, guys, but in this case I actually do need the "code golf", for a certain reason. Feb 12, 2013 at 14:58

2 Answers 2

7

Not sure I got the question, however:

$.browser.msie ? $('#myDiv').css({opacity: 1}) : $('#myDiv').animate({opacity: 1}, 300);

Also, I'm not an expert of IE anymore, but I wonder if you could do something like:

$('#myDiv').animate({opacity: 1}, $.browser.msie ? 0 : 300);

That should be applied immediately, and made it more readable with an external variable:

var speed = $.browser.msie ? 0 : 300;
$('#myDiv').animate({opacity: 1}, speed);

Or something like that.

7
  • @ZERO, this is awesome! I LOVE the idea of using var. The best of both worlds! Feb 12, 2013 at 15:09
  • So what exactly doesn't work means? How IE behave with this code? As I said I'm not an IE expert anymore, because I don't use this browser for my job, but in other browser the code behaves as expected – with speed 0 the changes is immediate.
    – ZER0
    Feb 12, 2013 at 15:18
  • 1
    I tried and it works for me: codepen.io/anon/pen/smJaD The only thing here is that $.browser is deprecated and removed in jQuery 1.9, so you have to use another version. But that is true with all solution that uses $.browser.
    – ZER0
    Feb 12, 2013 at 15:34
  • In my case I tried with mozilla instead of msie, so using Firefox makes it visible immediately without animation; and others is displayed with the animation.
    – ZER0
    Feb 12, 2013 at 15:38
  • 1
    Ehe, in page where you used the code I proposed, you forgot to actually wrap it on $(window).load(function() {…} that's why it doesn't work: $('#gallery') is executed too soon when the element is not loaded yet probably, that's why you don't see anything. Just wrap the code in the $(window).load(function() {…} and everything should works as the other version.
    – ZER0
    Feb 12, 2013 at 15:51
4

You can do it like this:

$(window).load(function() {
    $.browser.msie ? $('#myDiv').css({opacity:1}); : $('#myDiv').animate({opacity: 1}, 300);
});

I wouldn't recommend it for at least two reasons (browser sniffing, and readability), but you can.

Another way:

$(window).load(function() {
    $('#myDiv')[$.browser.msie ? 'css' : 'animate']({opacity:1}, $.browser.msie ? undefined : 300);
});

...but that's even worse. ;-)

8
  • Thank you. I understand the point about not recommending it, I just needed to know how this can be done. Feb 12, 2013 at 14:59
  • May I ask, @T.J. Crowder, why ae you against browser sniffing in this case? Feb 12, 2013 at 15:02
  • 1
    @DimitriVorontzov: I'm not against browser sniffing in this case specifically, I'm opposed to it generally. :-) There's almost (almost) always a better option (feature sniffing, for instance). Feb 12, 2013 at 15:03
  • Ok, @T.J. Crowder, then maybe you could suggest a solution (I'd be grateful): I'm sniffing the browser because IE does a lousy job animating png transparancy. #myDiv in this case contains a png image with transparency. What would you do? Feb 12, 2013 at 15:06
  • 1
    @DimitriVorontzov: Dunno, I've never animated png transparency. :-) But consider: What if IE10 does a really good job of it? But yes, behaviors like that can be hard to sniff. Kangax has a great list here: kangax.github.com/cft You never know, sometimes things are more sniffable than they seem. Feb 12, 2013 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.