<?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>Jonathan Weiß &#187; HTML</title>
	<atom:link href="http://mysterycity.de/blog/category/html/feed" rel="self" type="application/rss+xml" />
	<link>http://mysterycity.de/blog</link>
	<description>Economic Science and Information Technology</description>
	<lastBuildDate>Tue, 01 Dec 2009 09:51:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reading custom HTML attributes</title>
		<link>http://mysterycity.de/blog/reading-custom-html-attributes</link>
		<comments>http://mysterycity.de/blog/reading-custom-html-attributes#comments</comments>
		<pubDate>Wed, 25 Jul 2007 19:06:15 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[Opera]]></category>
		<category><![CDATA[Safari]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/reading-custom-html-attributes</guid>
		<description><![CDATA[Sometimes you need to store some extra information for an HTML element, which provides additional data to your scripts. The best way to store this information is to use it as an attribute value on the element itself.
You could use a special CSS class name for a group of elements which should be handled identically, [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to store some extra information for an HTML element, which provides additional data to your scripts. The best way to store this information is to use it as an attribute value on the element itself.<br />
You could use a special CSS class name for a group of elements which should be handled identically, but you might lose the flexibility of CSS if these elements have completely different styles. Another possibility is to use custom (proprietary) attributes. The <a href="http://dojotoolkit.org/">Dojo Toolkit</a> makes massively use of this method. Please note that your document will not be valid (X)HTML, if you do so. Anyway, this can be quite useful but you have to be careful how to set and read the attributes.</p>
<p>At runtime an HTML element is presented as a DOM element object. You can add attributes by assigning them to the object.</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myElement = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'testDiv'</span><span style="color: #66cc66;">&#41;</span>;
myElement.<span style="color: #006600;">mood</span> = <span style="color: #3366CC;">'happy'</span>;</pre>
<p>What happens if you are dealing with custom attributes that are not assigned at runtime but on the sever side? You will notice two characteristics: at first, all attribute names are lowercased by the browser and secondly this will not work as you expect.</p>
<p>Imagine you have this HTML element:
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;testDiv&quot;</span> foo=<span style="color: #ff0000;">&quot;bar&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p> and this script:
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myElement = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'testDiv'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>myElement.<span style="color: #006600;">foo</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>This alert box will show <em>undefined</em>. It seems that only HTML attributes which are described in the HTML specification (such as <code>id</code>, <code>lang</code> or <code>title</code>) are applied as attributes to the DOM element object. Of course, there are exceptions for this rule: the Internet Explorer applies <strong>all</strong> attributes to the object.</p>
<p>I  made a comparision sheet showing which browser supports which behavior.<br />
<table style="width:100%;text-align:center;border:1px #D0D0D0 solid;margin-bottom:10px; margin-top:2px;padding:4px;" cellspacing="0" cellpadding="2" border="0">
<tr>
<td></td>
<td></td>
<td rowspan="2"><img src="http://mysterycity.de/blog/wp-content/uploads/icons/firefox.jpg" width="32" height="33"><br>Firefox 2</td>
<td rowspan="2"><img src="http://mysterycity.de/blog//wp-content/uploads/icons/opera.jpg" width="32" height="33"><br>Opera 9</td>
<td rowspan="2"><img src="http://mysterycity.de/blog//wp-content/uploads/icons/safari.jpg" width="32" height="33"><br> Safari 3</td>
<td rowspan="2"><img src="http://mysterycity.de/blog//wp-content/uploads/icons/internet_explorer.jpg" width="32" height="33"><br>IE 7</td>
</tr>
<tr>
<td style="white-space: nowrap;text-align:left;vertical-align:bottom;" colspan="2"> attribute set by assigning </td>
<td></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;width:20px;"></td>
<td style="white-space: nowrap;text-align:left;"> element.<em>attributeName</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="white-space: nowrap;text-align:left;"> element.getAttribute() </td>
<td> <em style="color:#D84E05;">null</em> </td>
<td> <em style="color:#D84E05;">null</em> </td>
<td> <em style="color:#D84E05;">null</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr>
<td style="white-space: nowrap;text-align:left;" colspan="2"> attribute set by setAttribute() </td>
<td colspan="4"></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="white-space: nowrap;text-align:left;"> element.<em>attributeName</em></code> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="text-align:left;white-space: nowrap;"> element.getAttribute() </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr>
<td style="text-align:left;" colspan="2"> attribute set on server side </td>
<td colspan="5"></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="text-align:left;white-space: nowrap;"> element.<em>attributeName</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="text-align:left;white-space: nowrap;"> element.getAttribute() </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
</table>
<p>So, if you want to use custom attributes while having HTML elements which are created on runtime <strong>and</strong> on the server side, there is only one possibility: use <code>setAttribute()</code> and <code>getAttribute()</code>. This combination works in every case and in every browser.</p>
]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/reading-custom-html-attributes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get the actual style value of an element</title>
		<link>http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element</link>
		<comments>http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element#comments</comments>
		<pubDate>Thu, 28 Jun 2007 07:00:47 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element</guid>
		<description><![CDATA[Golo asked me some time ago how to retrieve the actual style property value of an element. It is easy to get the value applied by the element's style attribute or the CSS class name. But the actual value is affected by many CSS rules which are applied to the element: element type, element's parents, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.goloroden.de">Golo</a> asked me some time ago how to retrieve the actual style property value of an element. It is easy to get the value applied by the element's <code>style</code> attribute or the CSS class name. But the actual value is affected by many CSS rules which are applied to the element: element type, element's parents, class name(s) and style attribute (which can, of course, be modified by JavaScript through runtime).</p>
<p><a href='http://mysterycity.de/blog/wp-content/uploads/2007/06/css_rule_example.png' title='CSS rules'><img src='http://mysterycity.de/blog/wp-content/uploads/2007/06/css_rule_example.thumbnail.png' alt='CSS rules' /></a><br />
<a href='http://mysterycity.de/blog/wp-content/uploads/2007/06/css_rule_example.png' title='CSS rules'>An example of different CSS rules overwriting each other.</a></p>
<p>Luckily we do not have to worry about this &mdash; it is the browser's job to apply all rules correctly. There are two ways of retrieving the actual style information. Those on the dark side use <code>element.currentStyle[style property]</code> to get the actual property value. All others use the WC3 method <code>window.getComputedStyle()</code> to get a <a href="http://webkit.org/docs/a00018.html">CSSStyleDecoration object</a> which can be accesed via <code>getPropertyValue(style property)</code>.</p>
<p>Note, that the proprietary <code>currentStyle()</code> method has the same syntax as in your CSS files (e.g. <code>background-color</code>) while W3C DOM method uses the syntax you know from JavaScript (e.g. <code>backgroundColor</code>).</p>
]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
