Forum Discussion

guoguo's avatar
guoguo
Contributor
9 years ago
Solved

How to get the color of a textnode

Hi all,

 

I have a question about the colort of a textnode. Please look at the following two textnodes. I am trying to achieve their color by using the following code:

 

var pin7= Aliases.home.panel1.textnodePin7;

var cc= pin7.style.color;

var bc= pin7.style.backgroudcolor;

Log.Message(cc+bc);

 

But I always get the empty string for the colors? Which function is correct for achieving their colors?

 

 

fig1.png

 

Best Regards,

Jie

  • Not that simple usually I'm afraid.

     

    The colours are likey part of a stylesheet? (CSS layer)

     

    It's this you need to interpret in order to get the colours in use. And you'll sometimes find that different browsers report CSS differently. (I forget exactly how. But I have a feeling it may be transparencies or something? I certainly remember hitting a few differences between IE and Chrome.)

     

    And you may also find you run into transparencies in order to allow the colour from a object layered below it to show through.

     

    And sometimes that may be conditional. (ie - sometimes it will be transparent, sometimes not, depending on the logic in the code)

     

    And you may also have an opacity setting to factor in. (How see-through, or not, something is.)

     

    Here is the code I need to use (VBScript) to get the colour info back from the last web application I was working on:

     

    object_backgroundcolour =  <OBJECT>.ownerDocument.defaultView.getComputedStyle(<OBJECT>, "").backgroundColor

3 Replies

  • Not that simple usually I'm afraid.

     

    The colours are likey part of a stylesheet? (CSS layer)

     

    It's this you need to interpret in order to get the colours in use. And you'll sometimes find that different browsers report CSS differently. (I forget exactly how. But I have a feeling it may be transparencies or something? I certainly remember hitting a few differences between IE and Chrome.)

     

    And you may also find you run into transparencies in order to allow the colour from a object layered below it to show through.

     

    And sometimes that may be conditional. (ie - sometimes it will be transparent, sometimes not, depending on the logic in the code)

     

    And you may also have an opacity setting to factor in. (How see-through, or not, something is.)

     

    Here is the code I need to use (VBScript) to get the colour info back from the last web application I was working on:

     

    object_backgroundcolour =  <OBJECT>.ownerDocument.defaultView.getComputedStyle(<OBJECT>, "").backgroundColor
    • guoguo's avatar
      guoguo
      Contributor

      Hi,

       

      Thank you very much! With your help, I get the background color of the textnode. I found the example function for achieving the css attribute from smartbear webseite. And it works very well.

       

      function getStyle(element, styleProp)
      {
        var document = element.ownerDocument;
        if (aqObject.IsSupported(document, "defaultView"))
        {
          // Internet Explorer 9+, Firefox, Chrome, Opera
          var style = document.defaultView.getComputedStyle(element, "");
          return style[styleProp];
        }
        else
        {
          // Internet Explorer 7 - 8
          return element.currentStyle[styleProp];
        }
      }

       

      .....

      var backgroundColor = getStyle(myTextNode, "background-color");

       

       

      Best Regards,

       

    • royd's avatar
      royd
      Regular Contributor

      Hi Colin

       

      Thank you so much for a simple solution! In our case, I only have to worry about IE. This works perfectly for me.

       

      Thanks.