<?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>Code Couch &#187; Events</title>
	<atom:link href="http://www.codecouch.com/tag/events/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codecouch.com</link>
	<description>The ramblings of two code monkeys</description>
	<lastBuildDate>Thu, 19 Nov 2009 15:47:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Replacement for &#8220;bind&#8221; or &#8220;bindAsEventListener&#8221; in jQuery</title>
		<link>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/</link>
		<comments>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 20:21:47 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=238</guid>
		<description><![CDATA[Do you need to use the equivalent of Prototype's "bind" or "bindAsEventListener" in jQuery, passing through "this" context? Read on for more!]]></description>
			<content:encoded><![CDATA[<p>After working for many years with the Prototype JS framework, I decided it was time to bite the bullet and learn jQuery.</p>
<p>There were 3 reasons, really&#8230; 1: I&#8217;ve just started a new project and don&#8217;t need most of the bloat Prototype comes with; 2: I can now make a judgement call when it comes to choosing a framework; and 3: It&#8217;s another skill on my CV <img src='http://www.codecouch.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> )</p>
<p>I&#8217;ve just spent the weekend reading <a href="http://www.amazon.co.uk/gp/product/1933988355?ie=UTF8&#038;tag=codcou-21&#038;linkCode=as2&#038;camp=1634&#038;creative=6738&#038;creativeASIN=1933988355">jQuery in Action</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=codcou-21&#038;l=as2&#038;o=2&#038;a=1933988355" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> cover-to-cover, and I can highly recommend it for intermediate or advanced developers looking to see what it offers.</p>
<p>The one thing I miss from Prototype however are the &#8220;bind&#8221; and &#8220;bindAsEventListener&#8221; commands which can set the context (&#8221;this&#8221;) of the callback &#8211; in my case, the instance of a class.</p>
<p>After a bit of searching, I found <a href="http://www.cosmocode.de/en/blogs/gohr/20060724110917/">this post by Andreas Gohr</a>. It was coderjoe&#8217;s comment that set the wheels in motion. He showed that you could rewrite this Prototype code:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">initialize<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	Event.<span class="me1">observe</span><span class="br0">&#40;</span>someEl<span class="sy0">,</span> <span class="st0">'click'</span><span class="sy0">,</span>
		<span class="kw1">this</span>.<span class="me1">doSomething</span>.<span class="me1">bindAsEventListener</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>
	<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div>

<p>as this jQuery code:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">init<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw2">var</span> self <span class="sy0">=</span> <span class="kw1">this</span><span class="sy0">;</span>
	$<span class="br0">&#40;</span>someEl<span class="br0">&#41;</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>eventData<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		self.<span class="me1">doSomething</span><span class="br0">&#40;</span>eventData<span class="sy0">,</span> self<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div>

<p>The bind (and related) method allows you to pass three parameters, however: the event type, some data, and a callback function. If the data parameter is omitted, the callback can be put as the second parameter.</p>
<p>So, we can re-write that last example using the second data parameter, removing the need to set a scoped variable:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">init<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	jQuery<span class="br0">&#40;</span>someEl<span class="br0">&#41;</span>.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="sy0">,</span> <span class="br0">&#123;</span>thisContext<span class="sy0">:</span><span class="kw1">this</span><span class="br0">&#125;</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>eventData<span class="br0">&#41;</span> <span class="br0">&#123;</span>
		eventData.<span class="me1">data</span>.<span class="me1">thisContext</span>.<span class="me1">doSomething</span><span class="br0">&#40;</span>eventData<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/12/replacement-for-bind-or-bindaseventlistener-in-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to prevent page reloading with a non-submitted form</title>
		<link>http://www.codecouch.com/2007/08/how-to-prevent-page-reloading-with-a-non-submitted-form/</link>
		<comments>http://www.codecouch.com/2007/08/how-to-prevent-page-reloading-with-a-non-submitted-form/#comments</comments>
		<pubDate>Tue, 07 Aug 2007 11:12:03 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Forms]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=126</guid>
		<description><![CDATA[This technique informs the user if they navigate away without submitting the form on the page (whilst letting them change their mind and remain on the page). Useful to prevent the loss of unsaved changes on a web form.]]></description>
			<content:encoded><![CDATA[<p>The following example page shows how you can test whether a form has been changed, and inform the user if they navigate away from the page without submitting the form (whilst letting them change their mind and remain on the page).</p>
<p>This technique is suitable for IE6, IE7 and Firefox and relies on the <b>onbeforeunload</b> event to allow the user to change their minds and remain on the page (therefore allowing them to submit the form and not lose data).</p>
<p>Note the <b>onsubmit</b> event being added to the form during the initialisation process &#8211; this is to disable the <span style="font-weight: bold;">onbeforeunload</span> event from running the preUnload() method when the form is actually submitted.</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="html4strict" style="color: #000000;"><span class="sc2"><span class="kw2">&lt;html&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;head&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;title&gt;</span></span>Prevent accidental page reload<span class="sc2"><span class="kw2">&lt;/title&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;script</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="kw2">&gt;</span></span>
/* check to see if form field values have changed */
function checkForFormChanges()
{
  var l_oForm = document.getElementById('mainForm');
  var l_pInputs = l_oForm.getElementsByTagName('INPUT');
  for (var loop=0, max=l_pInputs.length; loop<span class="sc2">&lt;max; loop++<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    if <span class="br0">&#40;</span>l_pInputs<span class="br0">&#91;</span>loop<span class="br0">&#93;</span>.defaultValue !<span class="sy0">=</span> l_pInputs<span class="br0">&#91;</span>loop<span class="br0">&#93;</span>.<span class="kw3">value</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
      g_bPreventUnload <span class="sy0">=</span> true;
      break;
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="sy0">/</span>* <span class="kw3">method</span> to run as the page is about to unload *<span class="sy0">/</span>
function preUnload<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
  checkForFormChanges<span class="br0">&#40;</span><span class="br0">&#41;</span>;
  if <span class="br0">&#40;</span>g_bPreventUnload<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    var l_sMessage <span class="sy0">=</span> <span class="st0">&quot;You made changes to the form. &quot;</span>;
    l_sMessage +<span class="sy0">=</span> <span class="st0">&quot;If you continue, they will be lost.&quot;</span>;
    try
    <span class="br0">&#123;</span>
      window.event.returnValue <span class="sy0">=</span> l_sMessage;
    <span class="br0">&#125;</span>
    catch<span class="br0">&#40;</span>e<span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
      <span class="sy0">//</span> swallow firefox error
    <span class="br0">&#125;</span>
    return l_sMessage;
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="sy0">/</span>* <span class="kw3">clear</span> onbeforeunload event when the form is sent *<span class="sy0">/</span>
function clearUnload<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
  if <span class="br0">&#40;</span>window.detachEvent<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    window.detachEvent<span class="br0">&#40;</span><span class="st0">&quot;onbeforeunload&quot;</span>, preUnload<span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  else
  <span class="br0">&#123;</span>
    window.onbeforeunload <span class="sy0">=</span> null;    
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="sy0">/</span>* initialise the page, attaching the DOM events *<span class="sy0">/</span>
function init<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
  window<span class="br0">&#91;</span><span class="st0">'g_bPreventUnload'</span><span class="br0">&#93;</span> <span class="sy0">=</span> false;
  if <span class="br0">&#40;</span>window.attachEvent<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    window.attachEvent<span class="br0">&#40;</span><span class="st0">&quot;onbeforeunload&quot;</span>, preUnload<span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
  else
  <span class="br0">&#123;</span>
    window.onbeforeunload <span class="sy0">=</span> preUnload;
  <span class="br0">&#125;</span>
  document.getElementById<span class="br0">&#40;</span><span class="st0">'mainForm'</span><span class="br0">&#41;</span>.<span class="kw3">onsubmit</span> <span class="sy0">=</span> clearUnload;
<span class="br0">&#125;</span>
&nbsp;
window.<span class="kw3">onload</span> <span class="sy0">=</span> init;
<span class="kw2">&lt;/script&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/head&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;body&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;form</span> <span class="kw3">action</span><span class="sy0">=</span><span class="st0">&quot;&quot;</span> <span class="kw3">method</span><span class="sy0">=</span><span class="st0">&quot;get&quot;</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;mainForm&quot;</span><span class="kw2">&gt;</span></span>
  <span class="sc2"><span class="kw2">&lt;fieldset&gt;</span></span>
    <span class="sc2"><span class="kw2">&lt;input</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text&quot;</span> <span class="kw3">name</span><span class="sy0">=</span><span class="st0">&quot;reason&quot;</span> <span class="kw3">value</span><span class="sy0">=</span><span class="st0">&quot;initial&quot;</span><span class="sy0">/</span><span class="kw2">&gt;</span></span>
    <span class="sc2"><span class="kw2">&lt;input</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;submit&quot;</span> <span class="kw3">name</span><span class="sy0">=</span><span class="st0">&quot;submit&quot;</span> <span class="kw3">value</span><span class="sy0">=</span><span class="st0">&quot;Send Form&quot;</span><span class="sy0">/</span><span class="kw2">&gt;</span></span>
  <span class="sc2"><span class="kw2">&lt;/fieldset&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/form&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/body&gt;</span></span>
<span class="sc2"><span class="kw2">&lt;/html&gt;</span></span></pre></div></div></div>

<p>If the contents of the form field in the example above is changed and the page is reloaded (or the user tries to visit another URL), a message will show that allows the user to cancel the page being reloaded etc. This will not happen if the form field contents are not changed (nor when the form is submitted).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2007/08/how-to-prevent-page-reloading-with-a-non-submitted-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
