SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    if( internet-explorer ) { } else { } code

    i want to run a bit of js not in ie but in all others within some js i'm doing.

    is this the way to do it? (got this from o'reilly's js;definitive guide book):

    Code:
    /*@cc_on
    	@if( @_jscript )
    		; // nothing for internet explorer
    	@else*/
    		alert("you're not using internet explorer");
    /*@end
    	@*/
    how should braces be in that, if they should at all? i mean if i wanted multiple lines of js code in the either non-ie or ie blocks how and where would {}'s go?

    also does that work for ie8?

    thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i've just thought the braces part isn't too important because i want to do ie or not ie twice so it'd be better to do this anyway:
    Code:
    var ie = false;
    /*@cc_on
      @if( @_jscript )
          ie = true;
    /*@end
      @*/
    then just normal if(ie) { .. } else { .. } later on.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    No {} around the content is required but placing them there wouldn't affect anything. Basically the jScript conditional comments determine which code the browser sees rather than the if statement being run as a part of the code. It is therefore more efficient to use it where-ever you need to distinguish between IE and other browsers (or more specifically JScript and JavaScript) rather than setting a field to test within the code.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i see, thanks. and this works for ie8 as well does it? all ie's basically? plus any other browser which uses jscript rather than javascript.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Internet Explorer 3+ runs JScript

    All other browsers that support client side scripting run JavaScript

    If you need to distinguish further you can test the JScript version. While it doesn't tell you what version of IE it is running on specifically (since JScript can be upgraded without needing to move to a new IE version) it would give an indication of what that version of JScript supports (which doesn't depend on which version of IE it is anyway).
    Last edited by felgall; Mar 5, 2009 at 19:09.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    great, i see, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •