<?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>Latent Motion &#187; javascript function</title>
	<atom:link href="http://www.latentmotion.com/tag/javascript-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.latentmotion.com</link>
	<description>Usability, Design and Front-Side Programming</description>
	<lastBuildDate>Tue, 27 Jul 2010 05:40: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>Separating jQuery Functions into External Files (without selectors!)</title>
		<link>http://www.latentmotion.com/separating-jquery-functions-into-external-files-without-selectors/</link>
		<comments>http://www.latentmotion.com/separating-jquery-functions-into-external-files-without-selectors/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 13:00:54 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[external javascript]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript files]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[jquery function]]></category>
		<category><![CDATA[separating javascript]]></category>

		<guid isPermaLink="false">http://www.latentmotion.com/?p=336</guid>
		<description><![CDATA[In javascript, writing reusable blocks of code is easy. You simply enter something like: &#8220;function myFunction() { &#8230; }&#8221;, and then you later call it by simply writing &#8220;myFunction();&#8221;. Consequently, when I write jQuery, I often use this javascript shorthand within my jQuery ready tags. As long as all the code stays in a single [...]]]></description>
			<content:encoded><![CDATA[<p>In javascript, writing reusable blocks of code is easy. You simply enter something like: &#8220;function myFunction() { &#8230; }&#8221;, and then you later call it by simply writing &#8220;myFunction();&#8221;. Consequently, when I write jQuery, I often use this javascript shorthand within my jQuery ready tags. As long as all the code stays in a single file, things go smoothly.</p>
<p>But sometimes code grows, and when it does, you&#8217;ve got to separate your logic into separate external js files. That&#8217;s when things break. You see, in each of these external files, you now have to separately reiterate the jQuery ready tags, and when you do, you&#8217;ve buried your functions in a (jQuery) function, rendering it hidden. Out of scope. Or as the error console will tell you, &#8220;Missing&#8221;.</p>
<p>The trick is to create a jQuery function. There are articles all over the place that tell you how to do this. But every one that I&#8217;ve found seems to assume that you want your function to act on a selector. What I needed (and you may as well), was a function that acted just like that regular old javascript function (with jQuery syntax enabled). Believe it or not, all it takes is a minor tweek. Check it out:</p>
<p>External File (tilex.js):</p>
<pre><pre class="brush: jscript;">
  (function($){
    tile = function() {
      $(&quot;body&quot;).append('See? It works.');
    };
  })(jQuery);
</pre></pre>
<p>Inline HTML:</p>
<pre><pre class="brush: xml;">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;HyperStudio Timeline&lt;/title&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;js/tilex.js&quot;&gt;&lt;/script&gt;

  &lt;script type=&quot;text/javascript&quot;&gt;
   $(document).ready(function(){
      tile();
    });
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre></pre>




	<a rel="nofollow" class="digg" target="_blank" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.latentmotion.com%252Fseparating-jquery-functions-into-external-files-without-selectors%252F%26amp%3Btitle%3DSeparating%2520jQuery%2520Functions%2520into%2520External%2520Files%2520%2528without%2520selectors%2521%2529%26amp%3Bbodytext%3DIn%2520javascript%252C%2520writing%2520reusable%2520blocks%2520of%2520code%2520is%2520easy.%2520You%2520simply%2520enter%2520something%2520like%253A%2520%2522function%2520myFunction%2528%2529%2520%257B%2520...%2520%257D%2522%252C%2520and%2520then%2520you%2520later%2520call%2520it%2520by%2520simply%2520writing%2520%2522myFunction%2528%2529%253B%2522.%2520Consequently%252C%2520when%2520I%2520write%2520jQuery%252C%2520I%2520often%2520use%2520this%2520javascript%2520sho';" title="Digg"><img src="http://www.latentmotion.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" class="sphinn" target="_blank" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.latentmotion.com%252Fseparating-jquery-functions-into-external-files-without-selectors%252F';" title="Sphinn"><img src="http://www.latentmotion.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a>
	<a rel="nofollow" class="del.icio.us" target="_blank" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.latentmotion.com%252Fseparating-jquery-functions-into-external-files-without-selectors%252F%26amp%3Btitle%3DSeparating%2520jQuery%2520Functions%2520into%2520External%2520Files%2520%2528without%2520selectors%2521%2529%26amp%3Bnotes%3DIn%2520javascript%252C%2520writing%2520reusable%2520blocks%2520of%2520code%2520is%2520easy.%2520You%2520simply%2520enter%2520something%2520like%253A%2520%2522function%2520myFunction%2528%2529%2520%257B%2520...%2520%257D%2522%252C%2520and%2520then%2520you%2520later%2520call%2520it%2520by%2520simply%2520writing%2520%2522myFunction%2528%2529%253B%2522.%2520Consequently%252C%2520when%2520I%2520write%2520jQuery%252C%2520I%2520often%2520use%2520this%2520javascript%2520sho';" title="del.icio.us"><img src="http://www.latentmotion.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" class="rss" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.latentmotion.com%2Ffeed%2F';" title="RSS"><img src="http://www.latentmotion.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow" class="twitter" target="_blank" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DSeparating%2520jQuery%2520Functions%2520into%2520External%2520Files%2520%2528without%2520selectors%2521%2529%2520-%2520http%253A%252F%252Fwww.latentmotion.com%252Fseparating-jquery-functions-into-external-files-without-selectors%252F';" title="Twitter"><img src="http://www.latentmotion.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.latentmotion.com/separating-jquery-functions-into-external-files-without-selectors/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
