<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rtraction - London, Ontario - Web Design, Web Development and Strategic Consulting &#187; JavaScript</title>
	<atom:link href="http://www.rtraction.com/blog/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.rtraction.com/blog</link>
	<description>rtraction blog - Web Design, Web Development and Strategic Consulting</description>
	<lastBuildDate>Fri, 25 Nov 2011 15:08:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Looking up CSS Styles in JavaScript</title>
		<link>http://www.rtraction.com/blog/devit/looking-up-css-styles-in-javascript.html</link>
		<comments>http://www.rtraction.com/blog/devit/looking-up-css-styles-in-javascript.html#comments</comments>
		<pubDate>Fri, 17 Apr 2009 14:30:04 +0000</pubDate>
		<dc:creator>David Millar</dc:creator>
				<category><![CDATA[Devit!]]></category>
		<category><![CDATA[CrossBrowser]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Styles]]></category>

		<guid isPermaLink="false">http://www.rtraction.com/blog/?p=215</guid>
		<description><![CDATA[This can be extremely useful in the event you have a toolbar, menu, or custom piece of code that is changing styles on the fly. We had a need to look up CSS Styles affecting an object on our page with JavaScript.  Unfortunately, it&#8217;s not as easy as document.getElementById(&#8216;myobject&#8217;).style.background.  If you want to get the [...]]]></description>
			<content:encoded><![CDATA[<p>This can be extremely useful in the event you have a toolbar, menu, or custom piece of code that is changing styles on the fly. We had a need to look up CSS Styles affecting an object on our page with JavaScript.  Unfortunately, it&#8217;s not as easy as <strong>document.getElementById(&#8216;myobject&#8217;).style.background</strong>.  If you want to get the CSS styles that have been set by other JavaScript code, you&#8217;ll need to use this function to look up those styles.   Let&#8217;s say you want to put a div at the top of the page, but doing so would break the background styles on the body.  You can look up those styles, make an adjustment for the height of your div and then display it.</p>
<p>One thing to note is that IE and Firefox (as well as other W3C standards browsers) use a different function to accomplish this.  The W3C standard function is &#8220;getComputedStyle&#8221; whereas IE uses the proprietary &#8220;currentStyle&#8221;.</p>
<p>A working script for IE and Firefox is below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">getstylebanner <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>prop<span style="color: #339933;">,</span> element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">style</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> element.<span style="color: #660066;">style</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* if its an inline style in IE */</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">currentStyle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> element.<span style="color: #660066;">currentStyle</span><span style="color: #009900;">&#91;</span>prop<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/* if its style set in IE */</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">defaultView</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> document.<span style="color: #660066;">defaultView</span>.<span style="color: #660066;">getComputedStyle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* Firefox */</span>
        prop <span style="color: #339933;">=</span> prop.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/([A-Z])/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;-$1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        prop <span style="color: #339933;">=</span> prop.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> document.<span style="color: #660066;">defaultView</span>.<span style="color: #660066;">getComputedStyle</span><span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getPropertyValue</span><span style="color: #009900;">&#40;</span>prop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>An example of looking up the top margin on the body of your page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> marginTop <span style="color: #339933;">=</span> getstylebanner<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marginTop'</span><span style="color: #339933;">,</span> document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.rtraction.com/blog/devit/looking-up-css-styles-in-javascript.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

