style property IE vs Netscape

Discussion in 'Javascript' started by Shawn Modersohn, Aug 15, 2004.

  1. <script type="text/javascript">
    var browser=navigator.appName
    if (browswer="Microsoft Internet Explorer")
    document.styleSheets[0].rules[4].style.left="50px"
    </script>

    IE does exactely what I hoped this script would do. Netscape says that
    document.styleSheets[0].rules has no properties. Since this script is
    targeted at IE anyway, should I be concerned? Also, is this script way
    oversimplified? All the css rule does is shift an image slightly to
    compensate for css overflow differences in the two browsers.
     
    Shawn Modersohn, Aug 15, 2004
    #1
    1. Advertising

  2. Shawn Modersohn <> writes:

    > <script type="text/javascript">
    > var browser=navigator.appName
    > if (browswer="Microsoft Internet Explorer")


    Using "appName" to detect the browser is not reliable.
    I am using Opera. If I set it to emulate IE, it would also have
    an appName of "Microsoft Internet Explorer".

    > document.styleSheets[0].rules[4].style.left="50px"


    > IE does exactely what I hoped this script would do. Netscape says
    > that document.styleSheets[0].rules has no properties.


    That's because it doesn't. Mozilla (and therefore Netscape 6+)
    implements the W3C DOM 2 Style specification, so it would be
    document.styleSheets[0].CSSrules

    > Since this script is targeted at IE anyway, should I be concerned?


    It gives an error where there is no need for it.

    > Also, is this script way oversimplified? All the css rule does is
    > shift an image slightly to compensate for css overflow differences
    > in the two browsers.


    Then it's not oversimplified, but overcomplicated.
    Just use IE's conditional comments:
    ---
    <!--[if lt IE 7]>
    <style type='text/css'>
    .someImeage { left : 50px; }
    </style>
    <![endif]-->
    ---

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 15, 2004
    #2
    1. Advertising

  3. [minor] Re: style property IE vs Netscape

    On Sun, 15 Aug 2004 12:35:40 +0200, Lasse Reichstein Nielsen
    <> wrote:

    [snip]

    > document.styleSheets[0].CSSrules


    cssRules

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail
     
    Michael Winter, Aug 15, 2004
    #3
  4. Re: [minor] Re: style property IE vs Netscape

    "Michael Winter" <> writes:

    > On Sun, 15 Aug 2004 12:35:40 +0200, Lasse Reichstein Nielsen
    > <> wrote:


    >> document.styleSheets[0].CSSrules


    > cssRules


    Doh! Thanks.
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 15, 2004
    #4
  5. Shawn Modersohn

    Randy Webb Guest

    Shawn Modersohn wrote:
    > <script type="text/javascript">
    > var browser=navigator.appName
    > if (browswer="Microsoft Internet Explorer")
    > document.styleSheets[0].rules[4].style.left="50px"
    > </script>
    >
    > IE does exactely what I hoped this script would do.


    Actually, it doesn't do what you *think* it does though.

    > Netscape says that document.styleSheets[0].rules has no properties.


    Thats because its taking the branch of the if that it shouldn't be,
    because you are doing an incorrect comparison. == is comparison, = is
    assignment. So your if statement is testing to see if it can set the
    string browser equal to "Microsoft Internet Explorer", which any browser
    should do and pass the test.

    The correct test would be (notwithstanding the problems mentioned in
    other posts about the appName):

    if (browser == '...'){


    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Randy Webb, Aug 15, 2004
    #5
  6. Shawn Modersohn

    Alberto Guest

    Rearrange the expression
    document.styleSheets[0].rules[4].style.left="50px"
    into:

    if(document.styleSheets){
    document.styleSheets[0][
    (document.styleSheets[0].rules)?"rules":"cssRules"
    ][4].style.left="50px";
    };


    This grants a level of compatibility without browser sniffing.

    PS if by chance you add a style sheet node before that style sheet [0], or a
    css rule before rule [4], you won't arguably get anymore what you expect: if
    you're going to use that indexing, be sure the structure of your style
    sheets in your page never changes BEFORE index 4 of the rule, nor before
    index 0 of the style sheet itself.

    ciao
    Alberto
    http://www.unitedscripters.com/






    "Shawn Modersohn" <> ha scritto nel messaggio
    news:4RGTc.853$...
    > <script type="text/javascript">
    > var browser=navigator.appName
    > if (browswer="Microsoft Internet Explorer")
    > document.styleSheets[0].rules[4].style.left="50px"
    > </script>
    >
    > IE does exactely what I hoped this script would do. Netscape says that
    > document.styleSheets[0].rules has no properties. Since this script is
    > targeted at IE anyway, should I be concerned? Also, is this script way
    > oversimplified? All the css rule does is shift an image slightly to
    > compensate for css overflow differences in the two browsers.
    >
     
    Alberto, Aug 16, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom
    Replies:
    0
    Views:
    507
  2. Mickey Segal
    Replies:
    1
    Views:
    681
    Mickey Segal
    May 21, 2004
  3. Ken Varn
    Replies:
    0
    Views:
    506
    Ken Varn
    Apr 26, 2004
  4. Sky
    Replies:
    2
    Views:
    303
    Robert Koritnik
    Oct 1, 2004
  5. yaktipper
    Replies:
    10
    Views:
    297
    Richard Cornford
    Jul 10, 2003
Loading...

Share This Page