<?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>WebDev &#187; JavaScript</title>
	<atom:link href="http://webdev.gold.lv/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://webdev.gold.lv</link>
	<description>Dažādi pieraksti par dažādām lietām</description>
	<lastBuildDate>Wed, 07 Jan 2009 17:27:44 +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>Unobtrusive Javascript</title>
		<link>http://webdev.gold.lv/2008/11/unobtrusive-javascript/</link>
		<comments>http://webdev.gold.lv/2008/11/unobtrusive-javascript/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 14:04:13 +0000</pubDate>
		<dc:creator>M</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript html]]></category>

		<guid isPermaLink="false">http://webdev.gold.lv/?p=14</guid>
		<description><![CDATA[Noformējumam ir jābūt atdalītam no satura! Lūk tieši tādēļ mums ir iespēja web-lapas HTML failā glabāt tikai strukturizētu saturu, bet CSS un JavaScript pārvietot uz atsevišķiem failiem. Ieguvums ir ne tikai &#8220;tīrāks&#8221; lapas kods, kas atvieglo izstrādi un modificēšanu nākotnē, un ātrdarbību, bet tas ir arī pluss no SEO viedokļa, jo tādi meklētāji kā Google [...]]]></description>
			<content:encoded><![CDATA[<p>Noformējumam ir jābūt atdalītam no satura! Lūk tieši tādēļ mums ir iespēja web-lapas HTML failā glabāt tikai strukturizētu saturu, bet CSS un JavaScript pārvietot uz atsevišķiem failiem. Ieguvums ir ne tikai &#8220;tīrāks&#8221; lapas kods, kas atvieglo izstrādi un modificēšanu nākotnē, un ātrdarbību, bet tas ir arī pluss no SEO viedokļa, jo tādi meklētāji kā Google varēs vieglāk indeksēt saturu. Ar CSS viss ir vienkārši un skaidri &#8211; glabājam to atsevišķā failā un izvairāmies no &#8220;inline&#8221; stilu definēšanas (&lt;div style=&#8221;&#8230;&#8221;).</p>
<p>Arī JavaScript var pilnībā atdalīt no lapas satura un ne tikai glabāt to atsevišķos failos, bet arī nelietot tādus event`us kā onClick, onMouseOver, onLoad u.t.t. JavaScript web-lapā ir papildus funkcionalitāte un nedrīkst paļauties uz tā atbalstu lietotāja pārlūkprogrammā, jo tas var būt gan vienkārši atslēgts, gan izfiltrēts, gan netikt atbalstīts mobilajās ierīcēs.<br />
<span id="more-14"></span><br />
Agrāk mēs to darījām šādi:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mylink&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">onMouseOver</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;something(this);&quot;</span>&gt;</span>...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>Bet pareiza pieeja šajā gadījumā ir izvairīties no onMouseOver event`a (tā pat kā no jebkura cita) un atsevišķā javascript failā piemērot vajadzīgo papildus funkcionalitāti šim elementam:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mylink'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onMouseOver</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Kods, kurš izpildīsies pēc peles uzbīdīšanas uz saites</span>
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>Šāda pieeja ir arī nesalīdzināmi parocīgāka, ja vēlamies pievienot papildus funkcionalitāti uzreiz vairākiem elementiem. Tad ir nepieciešams atrast visus šos elementus (piemēram, pēc klases nosaukuma) un piešķirt tiem izpildāmo kodu. Piemērs:</p>
<p>HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img1.jpg&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;popupImg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;attēls1&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img2.jpg&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;popupImg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;attēls2&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img3.jpg&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;popupImg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;attēls3&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>JavaScript:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;">window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// Izpildam tikai pēc lapas ielādes</span>
   <span style="color: #003366; font-weight: bold;">var</span> nodes <span style="color: #339933;">=</span> getElementsByClass<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'popupImg'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// Atrodam visus elementus ar klasi &quot;popupImg&quot;</span>
   <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>0; i<span style="color: #339933;">&lt;</span>nodes.<span style="color: #660066;">length</span>; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      nodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #006600; font-style: italic;">// Norādam kas notiks pēc onClick event`a:</span>
         <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">alt</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Papildus funkcija, kas atgriež masīvu ar visiem vienas klases elementiem</span>
<span style="color: #003366; font-weight: bold;">function</span> getElementsByClass<span style="color: #009900;">&#40;</span> searchClass<span style="color: #339933;">,</span> domNode<span style="color: #339933;">,</span> tagName<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>domNode <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> domNode <span style="color: #339933;">=</span> document;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tagName <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> tagName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'*'</span>;
	<span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #003366; font-weight: bold;">var</span> tags <span style="color: #339933;">=</span> domNode.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span>tagName<span style="color: #009900;">&#41;</span>;
	<span style="color: #003366; font-weight: bold;">var</span> tcl <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">+</span>searchClass<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span>;
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>0<span style="color: #339933;">,</span>j<span style="color: #339933;">=</span>0; i<span style="color: #339933;">&lt;</span>tags.<span style="color: #660066;">length</span>; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> test <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> tags<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span>;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>test.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>tcl<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
			el<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> tags<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> el;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://webdev.gold.lv/2008/11/unobtrusive-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
