Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-16-2010, 09:25 PM   PM User | #1
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Question Getting the actual percentage value, not pixels for rule

Is there a way with Firefox/Chrome to get the actual % used instead of the rendered pixel values?

For example I have a table with the following CSS markup
Code:
#tableFoo : { width : 50%; }

In IE to get the percentage set in the CSS file, it is as simple as:

Code:
var width = tableObj.currentStyle["width"]
I am having trouble figuring out a way to get the percentage value with Firefox and Chrome. I am thinking it is not possible other than stylesheet sniffing and ripping out the rules that way which I am not going to do.

Thanks,
Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

Last edited by A1ien51; 04-16-2010 at 09:58 PM..
A1ien51 is offline   Reply With Quote
Old 04-16-2010, 11:47 PM   PM User | #2
TinyScript
Regular Coder

 
Join Date: Mar 2009
Location: Portland Oregon
Posts: 690
Thanks: 44
Thanked 63 Times in 62 Posts
TinyScript is on a distinguished road
the only way i could get it was if the style declaration was in the tag

Code:
<html><head>
<style>
#div{}

</style>

<script>
window.onload=function(){
var E = document.getElementById("div")
E.onmouseover=function(){alert(this.style.width)}
}
</script>
</head>
<body>

<div id="div"  style="width:50%;background-color:#ff0000"><p>Hello</p></div>
</body>
</html>
TinyScript is offline   Reply With Quote
Old 04-17-2010, 05:10 AM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Code:
document.percentWidth= function(who){
	var percent, pa= who.offsetParent,
	w1= pa.offsetWidth, w2= who.offsetWidth;
	percent= 100*(w2/w1);
	return percent.toFixed(2)+ "%";
}
This method will only work if the element is in the document tree, and not set to style.display='none'
mrhoo is offline   Reply With Quote
Users who have thanked mrhoo for this post:
TinyScript (04-17-2010)
Old 04-17-2010, 06:20 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
i don't like iterating though stylesheets either, but this function seems to be working.
since i don't like doing it, i took several steps to mitigate performance problems:
  • all rules are cached when the builder is parsed
  • @import styles don't get tested
  • rules are flattened and reversed so that the first hit wins
  • only elements with a defined class or id are sought
  • only rules ending with these types of css selectors are tested


if a partial match is found via the class or id, the selector's finality is tested by matching the selector with the element.

the first selector- element match is assumed to be the most recent, and it's style property is returned immediately.


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>css values?</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type='text/css'>
	#table1, #table2  { width : 50%; } 
</style>	
</head>
<body>
<div id='cont'>
	
  
  <table border='1' cellspacing='1' cellpadding='1' id="table1" >
	<tr>
		<th>aaa</th>
		<th>bbb</th>
	</tr>
	<tr>
		<td>ggg</td>
		<td>vvv</td>
	</tr>
</table>	

</div>	


<script type='text/javascript'>


var getElementDefinedStyle=(function gs(){
    function R(c){return [].slice.call(c);}
  var sheets=R(document.styleSheets),
    sels={},
    r=[];
    sheets.map(function(a){  
        r.splice.apply(r, [r.length, 0].concat(R(a.cssRules)));
    });
    r.reverse();


  return function getElementDefinedStyle(elm, prop){
   var rx=new RegExp( "(#"+elm.id+"(,|$))|(\\."+elm.className+"(,|$))" ,"i"), out="";
    r.some(function(a){  
       if(a.selectorText.match(rx) && document.querySelectorAll(a.selectorText)[0]==elm){
	  return out=a.style[prop];
	}
    });

 return out;
};


}());//end builder wrap



window.onload= function (){
	alert(	
		getElementDefinedStyle(
			document.getElementById("table1"), "width"
		)
	);
};
</script>
</body>
</html>
tested firefox 3.6


if you find something better, let me know.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 04-17-2010 at 06:29 AM..
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
A1ien51 (04-19-2010)
Old 04-17-2010, 07:28 AM   PM User | #5
TinyScript
Regular Coder

 
Join Date: Mar 2009
Location: Portland Oregon
Posts: 690
Thanks: 44
Thanked 63 Times in 62 Posts
TinyScript is on a distinguished road
I thought the other one worked pretty good, but I didn't understand the need for making it part of the document object. It works perfectly if you remove the margin of 20 px for internet explorer. I made a test with his function.

Code:
<html><head>
<style>

#div {width:50%;background-color:#ff0000;}

</style>

<script>

percentWidth= function(who){
var IEfudge = !window.addEventListener ? 20 : 0;
	var percent, pa= who.offsetParent,
	w1= pa.offsetWidth, w2= who.offsetWidth + IEfudge;
	percent= 100*(w2/w1);
	return percent.toFixed(2)+ "%";
}






window.onload=function(){
var E = document.getElementById("div")
E.onmouseover=function(){this.innerHTML="Hello  percent width = "+percentWidth(this);this.onmouseout=function(){this.innerHTML="Hello"}}
}
</script>
</head>
<body>

<div id="div"  ><p>Hello</p></div>
</body>
</html>
TinyScript is offline   Reply With Quote
Old 04-19-2010, 03:14 AM   PM User | #6
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Problem with MrHoo's solution is I have no clue if the developer assigns a percentage, ems, or px. It is a guessing game.

rnd me ran with the idea I had, I wanted to avoid it since I did not want to have to write the rules of CSS. I like what you did.

What I think I am going to do: My widget is going to assume ems, if it is different, the developer has to set a config in my widget. It sucks, but they should be using ems on our site anyway. lol

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Reply

Bookmarks

Tags
currentstyle, percentage, pixel, width

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 PM.


Advertisement
Log in to turn off these ads.