<?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>Thoughts on Design &#187; JavaScript</title>
	<atom:link href="http://www.jamischarles.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jamischarles.com/blog</link>
	<description>Thoughts on Web Design and Front-end Coding. Served up Monthly by Jamis Charles</description>
	<lastBuildDate>Fri, 06 Jan 2012 21:11:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Break; and continue; in javascript loops</title>
		<link>http://www.jamischarles.com/blog/break-and-continue-in-javascript-loops/</link>
		<comments>http://www.jamischarles.com/blog/break-and-continue-in-javascript-loops/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 14:40:22 +0000</pubDate>
		<dc:creator>jamis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Photoshop]]></category>

		<guid isPermaLink="false">http://www.bluebreeze.net/blog/?p=35</guid>
		<description><![CDATA[Break; and continue; statements are essential when working with complex loops. They can be very useful. You can use them to end a loop prematurely, or skip the rest of the loop to start over at the beginning. Here is how you implement it: &#60;script type="text/javascript"&#62; //build our array var array1 = new Array(); array1[0] [...]]]></description>
			<content:encoded><![CDATA[<p>Break; and continue; statements are essential when working with complex loops. They can be very useful. You can use them to end a loop prematurely, or skip the rest of the loop to start over at the beginning. Here is how you implement it:</p>
<pre class="js">&lt;script type="text/javascript"&gt;
//build our array
var array1 = new Array();
array1[0] = 0;
array1[1] = 1;
array1[2] = 2;
array1[3] = 3;
array1[4] = 4;

var numArray = array1.length;
for(var i=0; i&lt;numarray ; i++){
	if (array1[i] == 2){
		continue; //skip to the end of the for loop, then start over after incrementing by 1
	}else if (array1[i] == 3){
		break; //end the for loop, and execute code after the loop
	}

	alert("still in for loop: " + i);
}
	alert("after for loop: " + i);
&lt;/script&gt;</pre>
<p>The above code should have created popups for the numbers &#8220;0&#8243;, &#8220;1&#8243;, and &#8220;3&#8243;. This is because the &#8220;continue;&#8221; statement makes the code skip to the end of the loop, not executing any of the rest of the loop code, then starts over at the beginning of the loop after incrementing i by 1.</p>
<p>The &#8220;break;&#8221; statement causes the loop to end and the code after the loop to be executed. i is NOT incremented anymore and thus remains at 3.</p>
<p>You can also use &#8220;return;&#8221; to just end a function prematurely.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamischarles.com/blog/break-and-continue-in-javascript-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing a prototype function in JavaScript</title>
		<link>http://www.jamischarles.com/blog/writing-a-prototype-function-in-javascript/</link>
		<comments>http://www.jamischarles.com/blog/writing-a-prototype-function-in-javascript/#comments</comments>
		<pubDate>Sat, 21 Jun 2008 13:53:56 +0000</pubDate>
		<dc:creator>jamis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.bluebreeze.net/blog/?p=34</guid>
		<description><![CDATA[This has been tested in FF3, IE7. Say you wanted to add a function that would be inherent to an element, say you wanted to write an &#8220;arrayName.clone()&#8221; method (as found in java, but not JavaScript). Here&#8217;s how: &#60;script type="text/javascript"&#62; Array.prototype.clone = function (){ var newArray = new Array(this.length); for(var i=0; i &#60; this.length; i++ [...]]]></description>
			<content:encoded><![CDATA[<p>This has been tested in FF3, IE7.<br />
Say you wanted to add a function that would be inherent to an element, say you wanted to write an &#8220;arrayName.clone()&#8221; method (as found in java, but not JavaScript). Here&#8217;s how:</p>
<pre class="js">&lt;script type="text/javascript"&gt;
Array.prototype.clone = function (){
	var newArray = new Array(this.length);

	for(var i=0; i &lt; this.length; i++ ){
		newArray[i] = this[i];
	}

	return newArray;
}

&lt;/script&gt;</pre>
<p>This would have the same effect as the following code:</p>
<pre class="js">&lt;script type="text/javascript"&gt;
function cloneArray(oldArray){
	var newArray = new Array(oldArray.length);

	for(var i=0; i &lt; oldArray.length; i++ ){
		newArray[i] = oldArray[i];
	}

	return newArray;
}

&lt;/script&gt;</pre>
<p>The difference here being that for the prototype function you call it by using &#8220;arrayName.clone()&#8221; and for the second function you use cloneArray(arrayName). The both functions return the same thing.</p>
<p>Why not just use the following code below, you might ask? Because what you are doing below is just creating a new reference to the same array object. So if you made changes to oldArray, newArray would reflect those same changes. The functions above actually create and return a brand new array.</p>
<pre class="js">
var oldArray = new Array();
var newArray = oldArray;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jamischarles.com/blog/writing-a-prototype-function-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>document.getElementById().innerHTML + IE7 can cause problems</title>
		<link>http://www.jamischarles.com/blog/documentgetelementbyidinnerhtml-ie7-can-cause-problems/</link>
		<comments>http://www.jamischarles.com/blog/documentgetelementbyidinnerhtml-ie7-can-cause-problems/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 13:35:01 +0000</pubDate>
		<dc:creator>jamis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[IE7]]></category>

		<guid isPermaLink="false">http://www.bluebreeze.net/blog/?p=29</guid>
		<description><![CDATA[It seems that Firefox and IE7 can use and replace the innerHTML of a DIV just fine. However when you try it with a table or a tbody tag (among others, I&#8217;m sure), it just breaks apart miserably in IE7 (Firefox works just fine). You&#8217;ll most likely get a JavaScript error that states &#8220;Unknown runtime [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that Firefox and IE7 can use and replace the innerHTML of a DIV just fine. However when you try it with a table or a tbody tag (among others, I&#8217;m sure), it just breaks apart miserably in IE7 (Firefox works just fine). You&#8217;ll most likely get a JavaScript error that states &#8220;Unknown runtime error&#8221; and then point to the line that uses the innerHTML of the element that isn&#8217;t a DIV.</p>
<p>The solution? There are 2 possible workarounds. 1) Use a DIV, or 2) use appendChild(). I will show how to use appendChild here. I&#8217;ve tested this in IE7, FF, and Safari (Mac) and it should work. Thanks to <a href="http://domscripting.com/blog/display/99">Jeremy Keith</a> I was able to figure it out after hours of struggling.</p>
<p>The following code should probably be within a function you call.</p>
<pre class="js">&lt;script type="text/javascript"&gt;

var output = "dynamic output from earlier should go in this variable";

//first, create the table row and the table cell
var newtr = document.createElement("tr");
var newtd = document.createElement("td");

//place the output variable within the table cell, innerHTML here works because it's not on the page yet
//then, append the cell to the row, basically places the "&lt;td&gt;output here&lt;/td&gt;" tags within the &lt;tr&gt;&lt;/tr&gt; tags
newtd.innerHTML = output;
newtr.appendChild(newtd);

//last, append the whole thing to the element, here it'll find the element with the "tbody_one" id, go inside, and make it the last child
document.getElementById("tbody_one").appendChild(newtr);

&lt;/script&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jamischarles.com/blog/documentgetelementbyidinnerhtml-ie7-can-cause-problems/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to apply javascript to one specific browser (browser sniffer)</title>
		<link>http://www.jamischarles.com/blog/how-to-apply-javascript-to-one-specific-browser-browser-sniffer/</link>
		<comments>http://www.jamischarles.com/blog/how-to-apply-javascript-to-one-specific-browser-browser-sniffer/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 14:11:08 +0000</pubDate>
		<dc:creator>jamis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[Browser Sniffer]]></category>

		<guid isPermaLink="false">http://www.bluebreeze.net/blog/?p=28</guid>
		<description><![CDATA[What if you have JavaScript that only works in one browser? What if you&#8217;re using IE and Firefox. Well, you need to write one statement that will execute in IE, then another statement to run in the Firefox. This is also called a browser sniffer. Here&#8217;s how you can do it. First we&#8217;ll show you [...]]]></description>
			<content:encoded><![CDATA[<p>What if you have JavaScript that only works in one browser? What if you&#8217;re using IE and Firefox. Well, you need to write one statement that will execute in IE, then another statement to run in the Firefox. This is also called a browser sniffer.<br />
Here&#8217;s how you can do it.<br />
First we&#8217;ll show you how to write a catch so it&#8217;ll run 1 statement if it is IE, and another if it&#8217;s any other browser.</p>
<pre class="js">
&lt;script type="text/javascript"&gt;
var browser = navigator.appName; //find the browser name

alert(browser); //popup with the browser name

if(browser == "Microsoft Internet Explorer"){
    alert("You are using IE");
}else{
    alert("you are using a different browser than IE");
}
&lt;/script&gt;</pre>
<p>Usually the code above suffices, because most JavaScript will either work in IE or the other browsers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamischarles.com/blog/how-to-apply-javascript-to-one-specific-browser-browser-sniffer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to getElementsByClassName in JavaScript and manipulating those</title>
		<link>http://www.jamischarles.com/blog/how-to-execute-a-function-in-javascript-without-calling-it/</link>
		<comments>http://www.jamischarles.com/blog/how-to-execute-a-function-in-javascript-without-calling-it/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 13:19:06 +0000</pubDate>
		<dc:creator>jamis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.bluebreeze.net/blog/?p=24</guid>
		<description><![CDATA[What if you wanted all of your elements on the page that have the same class=&#8221;" to behave the exact same way. Well, with a little JavaScript, this is possible, thanks to Netlobo.com: Note: You must add the following onload event to your body tag: &#60; body onload=&#8221;loadClassNames()&#8221;&#62; &#60;script language="javascript" type="text/javascript"&#62; //function that allows us [...]]]></description>
			<content:encoded><![CDATA[<p>What if you wanted all of your elements on the page that have the same class=&#8221;" to behave the exact same way. Well, with a little JavaScript, this is possible, thanks to <a href="http://www.netlobo.com/javascript_getelementsbyclassname.html">Netlobo.com</a>:</p>
<p>Note: You must add the following onload event to your body tag: &lt; body onload=&#8221;loadClassNames()&#8221;&gt;</p>
<pre class="js">
&lt;script language="javascript" type="text/javascript"&gt;

//function that allows us to get the elements by a class name,

document.getElementsByClassName = function(clsName){

	var retVal = new Array();
	var elements = document.getElementsByTagName("*");

	for(var i = 0;i &lt; elements.length;i++){
		if(elements[i].className.indexOf(" ") &gt;= 0){
			var classes = elements[i].className.split(" ");

			for(var j = 0;j &lt; classes.length;j++){
				if(classes[j] == clsName){
					retVal.push(elements[i]);
				}
			}

		}else if(elements[i].className == clsName){
			retVal.push(elements[i]);
		}
	}
	return retVal;
}

function loadClassNames(){
	//this method only works for event handlers

	var classNames= document.getElementsByClassName('test2');

	for (var i = 0; i &lt; classNames.length; i++){
		classNames[i].onmouseover = function(){
			alert(this.id);
		}

	}
}

&lt;/script&gt;</pre>
<p>The first function simply adds the capability of getting an element by class name, which is not inherently possible with JavaScript. Now, what the 2nd function does, is the following. If you add loadClassNames() as an &#8220;onload&#8221; function to the body tag, when the page loads, the function will get all of the elements with a class name of &#8216;test2&#8242;, and assign a new &#8220;mouseover&#8221; event. In this case, it will create a popup that displays the id of the element.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamischarles.com/blog/how-to-execute-a-function-in-javascript-without-calling-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find x and y position of an element using JavaScript</title>
		<link>http://www.jamischarles.com/blog/find-x-and-y-position-of-an-element-using-javascript/</link>
		<comments>http://www.jamischarles.com/blog/find-x-and-y-position-of-an-element-using-javascript/#comments</comments>
		<pubDate>Thu, 01 Nov 2007 16:50:15 +0000</pubDate>
		<dc:creator>jamis</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.bluebreeze.net/blog/?p=27</guid>
		<description><![CDATA[Ever needed to find the x or y coordinates of an element on a page? Well, thanks to this script from quirksmode it&#8217;s possible.   function findPos(obj) { var curleft = curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft curtop = obj.offsetTop while (obj = obj.offsetParent) { curleft += obj.offsetLeft curtop += obj.offsetTop } [...]]]></description>
			<content:encoded><![CDATA[<p>Ever needed to find the x or y coordinates of an element on a page? Well, thanks to this script from <a href="http://www.quirksmode.org/js/findpos.html">quirksmode</a> it&#8217;s possible.</p>
<p> </p>
<pre class="js">function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}</pre>
<pre class="js">var elemToFind = document.getElementById('div1');

var elemCoords = findPos(elemToFind);</pre>
<p>So now, elemCoords holds both the x and the y. Since findPos() returns an array, to access the x or y by itself do the following:</p>
<pre class="js">// elemCoords[0] or elemCoords[1] for the x and y values, respectively
alert(elemCoords[0]);</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jamischarles.com/blog/find-x-and-y-position-of-an-element-using-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

