Basically, I have my DOM objects set up and am calling the .getAttribute method on an IMG element that is defined as below:
<IMG style="WIDTH: 134px; HEIGHT: 75px;" src="...">
Assuming hElement is the object reference to above element, when I call the following:
MsgBox hElement.getAttribute("style")
I get the following returned: "WIDTH" only, ie: part including and after the : character is ignored. To add to this, if I do a hElement.getAttribute("width") on the IMG element, it returns the actual width as "134" even though I don't explicitely have a width="..." attribute defined. So, basically, I am asking, how can I ensure I get back things as they are written, and not as they are reconstructed and stored by IE, as that is what it seems to be doing with the inferred WIDTH attribute. Also, not sure why it is ignoring everything including and after the : character right next to the WIDTH - how can I make this work properly?
Odd thing is, if I do the following, it shows everything as it should be:
hElement.outerHTML
The style attribute's value is shown as defined above. If I have to parse .outerHTML in order to get correct readings, that is just going to be depressing - I'm really disappointed in MS's half baked efforts. If you're looking for something extra to read, feel free to refer to another issue I noted about attributes returning odd behaviour when using capitals and when not (solved through a hack).
I'm using VB6, but it is all just the same, mshtml.dll, I gather, I am using IHTMLElement to define the hElement, not sure if I should be using something else but I think IHTMLElement is good for IE6+...?
UPDATE:
If I declare the hElement as IHTMLElement5 or IHTMLElement6 then it WORKS AS EXPECTED. If I use any of the following, it doesn't work: IHTMLElement, IHTMLElement2, IHTMLElement3 and IHTMLElement4. Since it only goes up to 6, only 5 & 6 work. It seems as though these are added later for newer versions of IE, and although you don't see the .getAttribute object in intellisense in 2 and above, you can still use it for some reason (not sure if its based on what IE version is installed). Does anyone know what version of IE IHTMLElement5 and IHTMLElement6 stands for? How can I get consistent behaviour for IE6+ as I don't have control on what versions are installed on the clients. If I use IHTMLElement5 or IHTMLElement6 does this mean it will not work on IE6 clients?
More: Turns out IHTMLElement5 is for IE8+ and IHTMLElement6 is for IE9+ - any ideas or remendations on how to get consistent behaviour for IE6+? Thanks.
UPDATE2: I have added a new question, which spawns from this question, it can be found here for those interested: What happens when using IHTMLElement5/6 (for IE8/9) in IE6/7? Does it divert to IHTMLElement behaviour?
Basically, I have my DOM objects set up and am calling the .getAttribute method on an IMG element that is defined as below:
<IMG style="WIDTH: 134px; HEIGHT: 75px;" src="...">
Assuming hElement is the object reference to above element, when I call the following:
MsgBox hElement.getAttribute("style")
I get the following returned: "WIDTH" only, ie: part including and after the : character is ignored. To add to this, if I do a hElement.getAttribute("width") on the IMG element, it returns the actual width as "134" even though I don't explicitely have a width="..." attribute defined. So, basically, I am asking, how can I ensure I get back things as they are written, and not as they are reconstructed and stored by IE, as that is what it seems to be doing with the inferred WIDTH attribute. Also, not sure why it is ignoring everything including and after the : character right next to the WIDTH - how can I make this work properly?
Odd thing is, if I do the following, it shows everything as it should be:
hElement.outerHTML
The style attribute's value is shown as defined above. If I have to parse .outerHTML in order to get correct readings, that is just going to be depressing - I'm really disappointed in MS's half baked efforts. If you're looking for something extra to read, feel free to refer to another issue I noted about attributes returning odd behaviour when using capitals and when not (solved through a hack).
I'm using VB6, but it is all just the same, mshtml.dll, I gather, I am using IHTMLElement to define the hElement, not sure if I should be using something else but I think IHTMLElement is good for IE6+...?
UPDATE:
If I declare the hElement as IHTMLElement5 or IHTMLElement6 then it WORKS AS EXPECTED. If I use any of the following, it doesn't work: IHTMLElement, IHTMLElement2, IHTMLElement3 and IHTMLElement4. Since it only goes up to 6, only 5 & 6 work. It seems as though these are added later for newer versions of IE, and although you don't see the .getAttribute object in intellisense in 2 and above, you can still use it for some reason (not sure if its based on what IE version is installed). Does anyone know what version of IE IHTMLElement5 and IHTMLElement6 stands for? How can I get consistent behaviour for IE6+ as I don't have control on what versions are installed on the clients. If I use IHTMLElement5 or IHTMLElement6 does this mean it will not work on IE6 clients?
More: Turns out IHTMLElement5 is for IE8+ and IHTMLElement6 is for IE9+ - any ideas or remendations on how to get consistent behaviour for IE6+? Thanks.
UPDATE2: I have added a new question, which spawns from this question, it can be found here for those interested: What happens when using IHTMLElement5/6 (for IE8/9) in IE6/7? Does it divert to IHTMLElement behaviour?
Share Improve this question edited May 23, 2017 at 12:12 CommunityBot 11 silver badge asked Mar 8, 2012 at 12:56 Erx_VB.NExT.CoderErx_VB.NExT.Coder 4,85210 gold badges60 silver badges93 bronze badges 2- You've mentioned C#, javascript, VB6, and VB.NET. Which are you using? – jrummell Commented Mar 8, 2012 at 13:59
- @jrummell I've tried it in all of them, – Erx_VB.NExT.Coder Commented Mar 8, 2012 at 16:11
2 Answers
Reset to default 2You might wanna take a look at this http://robertnyman./2006/04/24/get-the-rendered-style-of-an-element/
SO in your case you should try using the cssText property which will return a string consisting of all the css properties
hElement.style.cssText
Flags are not supported anymore? http://msdn.microsoft./en-us/library/ms536429%28v=vs.85%29.aspx
However, this works:
styleContent=hElement.style.cssText;
It seems, that getAttribute('style')
returns an object instead of the value of the attribute, since the code below works too.
styleContent=hElement.getAttribute('style').cssText;