SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy computed style works sometimes?

    I have been creating my own library and getting an element's computed style is at the top of my priority list. However, I am encountering some strange behavior. Here is some code. I know it isn't the most efficient but I've been maneuvering stuff around to troubleshoot.

    Code javascript:
    var Smola = {};
     
    Smola.getStyle = function(element, property) {
     
    	elem = document.getElementById(element);
    	if (elem != "undefined") {
    		alert('elem OK');
    		if (typeof elem.currentStyle != "undefined") {
    			alert('currentStyle OK');
    			var theStyle = elem.currentStyle[property];
    			return theStyle;
    		} else {
    			alert('currentStyle NOT ok');
    			return document.defaultView.getComputedStyle(elem,null)[property];
    		};
     
    	} else {
    		alert('Element does not exist!');
    	};
    };

    Code html4strict:
    <script type="text/javascript" src="scripts/smola.js"></script>
    <script type="text/javascript">
     
    function test() {
     
    		alert(Smola.getStyle('me','border'));
     
    };
     
    </script>
     
    <style type="text/css">
     
    #me {
    	border:1px solid black;
    	margin:10px 5px;
    }
     
    </style>
     
    </head>
     
    <body>
     
    <p id="me">Hello me.</p>
    <input type="button" value="Action" onclick="test();" />
     
    </body>
    </html>

    Alright, so when the style property I look for is 'margin', a string is returned with "10px 5px" as the value. But if I try 'border' the function returns 'undefined'. Why is this? Are there some styles that can't be returned since they can be broken down into other values? I realize there is a BUNCH that can be defined by just using the border style, and only 4 different things with the margin style. Does that have something to do with it?
    Humbly,

    Smola

  2. #2
    SitePoint Addict Smola's Avatar
    Join Date
    Mar 2005
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While waiting I continued to search the web for similar issues and I believe I found it. My original hypothesis was correct. If I want details about the border I would need to find the individual piece styles. So, in terms of the color I would use 'borderColor' and border width I would need to find 4 values: borderTopWidth, borderBottomWidth, etc.

    Maybe this will be useful to others...
    Humbly,

    Smola


Tags for this Thread

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
  •