Cascade problem with DOM & CSS?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pieter Van Waeyenberge

    #1

    Cascade problem with DOM & CSS?

    Hello

    consider:
    <html>
    <head>
    <style type="text/css">
    body{margin:50p x;}
    </style>
    ....
    <body> ...

    When i get a handle for the body (using DOM: doc.getElsByTag Name ...
    item(0))
    and i try to read style.margin from the body, i get an empty string

    However, when i specify the style info directly in the BODY element <body
    style="margin:5 0px;"> , and then i read out style.margin ... i get my value
    50px

    ? i dont know how the parsers work but after specifying style info whichever
    way, is should be available in the object model shouldnt it???

    Aparently both ie and ns behave the same on this ...

    Ultimately i want to position elements absolutely but 'offsetLeft/Top'
    doesnt relate to 'margin'

    PIeter.


  • Bas Cost Budde

    #2
    Re: Cascade problem with DOM &amp; CSS?

    Pieter Van Waeyenberge wrote:

    Do currentStyle (IE) and getComputedStyl e (NS/Moz) help you out?

    --
    Bas Cost Budde

    but the domain is nl

    Comment

    • Martin Honnen

      #3
      Re: Cascade problem with DOM &amp; CSS?



      Pieter Van Waeyenberge wrote:
      [color=blue]
      > consider:
      > <html>
      > <head>
      > <style type="text/css">
      > body{margin:50p x;}
      > </style>
      > ...
      > <body> ...
      >
      > When i get a handle for the body (using DOM: doc.getElsByTag Name ...
      > item(0))
      > and i try to read style.margin from the body, i get an empty string[/color]

      Yes,
      elementObject.s tyle
      only reflects what is set as an inline style (or later on with script),
      it it not a cascaded or a computed style value.
      IE provides
      elementObject.c urrentStyle
      W3C DOM compliant browsers like Mozilla or Opera 7 have
      window.getCompu tedStyle(elemen tObject,
      '').getProperty Value('css-property-name')

      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      • Pieter Van Waeyenberge

        #4
        Re: Cascade problem with DOM &amp; CSS?

        thnx!

        Pieter

        "Pieter Van Waeyenberge" <p@foo.com> wrote in message
        news:40279580$0 $322$ba620e4c@n ews.skynet.be.. .[color=blue]
        > Hello
        >
        > consider:
        > <html>
        > <head>
        > <style type="text/css">
        > body{margin:50p x;}
        > </style>
        > ...
        > <body> ...
        >
        > When i get a handle for the body (using DOM: doc.getElsByTag Name ...
        > item(0))
        > and i try to read style.margin from the body, i get an empty string
        >
        > However, when i specify the style info directly in the BODY element <body
        > style="margin:5 0px;"> , and then i read out style.margin ... i get my[/color]
        value[color=blue]
        > 50px
        >
        > ? i dont know how the parsers work but after specifying style info[/color]
        whichever[color=blue]
        > way, is should be available in the object model shouldnt it???
        >
        > Aparently both ie and ns behave the same on this ...
        >
        > Ultimately i want to position elements absolutely but 'offsetLeft/Top'
        > doesnt relate to 'margin'
        >
        > PIeter.
        >
        >[/color]


        Comment

        • DU

          #5
          Re: Cascade problem with DOM &amp; CSS?

          Pieter Van Waeyenberge wrote:
          [color=blue]
          > Hello
          >
          > consider:
          > <html>
          > <head>
          > <style type="text/css">
          > body{margin:50p x;}
          > </style>
          > ...
          > <body> ...
          >
          > When i get a handle for the body (using DOM: doc.getElsByTag Name ...
          > item(0))[/color]

          HTML documents only allow 1 body element. So, parsing the collection of
          body nodes unneedlessly takes time to be executed. Just
          document.body
          access the body node immediately. Much more efficient that way.
          [color=blue]
          > and i try to read style.margin from the body, i get an empty string
          >
          > However, when i specify the style info directly in the BODY element <body
          > style="margin:5 0px;"> , and then i read out style.margin ... i get my value
          > 50px
          >
          > ? i dont know how the parsers work but after specifying style info whichever
          > way, is should be available in the object model shouldnt it???[/color]

          There is nothing to parse really. Just to access.
          [color=blue]
          >
          > Aparently both ie and ns behave the same on this ...
          >
          > Ultimately i want to position elements absolutely but 'offsetLeft/Top'
          > doesnt relate to 'margin'[/color]

          You'll need to take under consideration the borders on the root element
          for MSIE 6 in standards compliant rendering mode and the padding on the
          body for Opera 7.x
          [color=blue]
          >
          > PIeter.
          >
          >[/color]


          offsetLeft and offsetTop are DOM extensions and refer to MSIE's DHTML
          object model. offsetLeft and offsetTop have not per se to do with css
          rules, stylesheets, css properties.

          Here's how to access the margin property values as set in a local
          stylesheet (such stylesheet must be the only stylesheet, otherwise you
          need to set the index accordingly: document.styleS heets[index])

          if(document.sty leSheets[0] && "cssRules" in document.styleS heets[0])
          // DOM 2 Stylesheet compliant
          {
          alert("Margins on the body are: " +
          document.styleS heets[0].cssRules[0].style.margin);
          }
          else if(document.sty leSheets[0] && "rules" in document.styleS heets[0])
          // MSIE
          {
          alert("Margins on the body are: " +
          document.styleS heets[0].rules[0].style.margin);
          };

          getComputedStyl e is good only when the margins have not been set or have
          been modified dynamically by the user.

          DU

          Comment

          Working...