<?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>Think Bohemian &#187; clickable</title>
	<atom:link href="http://www.thinkbohemian.com/tag/clickable/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thinkbohemian.com</link>
	<description>Secede from Convention in Life and Code</description>
	<lastBuildDate>Tue, 31 Aug 2010 23:37:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Global Javascript Variables and You</title>
		<link>http://www.thinkbohemian.com/2009/10/17/global-javascript-variables-and-you/</link>
		<comments>http://www.thinkbohemian.com/2009/10/17/global-javascript-variables-and-you/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 23:31:52 +0000</pubDate>
		<dc:creator>Richard Schneeman</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[clickable]]></category>
		<category><![CDATA[flag]]></category>
		<category><![CDATA[global]]></category>
		<category><![CDATA[table row]]></category>
		<category><![CDATA[window]]></category>

		<guid isPermaLink="false">http://www.thinkbohemian.com/?p=123</guid>
		<description><![CDATA[Just found out today that you can attach a javascript variable to the  window effectively making it a variable global to the window. You can define it like this:
window.myboolean = false ;
And then you can access the same variable anywhere in your window. Pretty Cool Huh? The reason I needed a global variable in the [...]]]></description>
			<content:encoded><![CDATA[<p>Just found out today that you can attach a javascript variable to the  <strong>window </strong>effectively making it a variable global to the window. You can define it like this:</p>
<pre>window.myboolean = false ;</pre>
<p>And then you can access the same variable anywhere in your window. Pretty Cool Huh? The reason I needed a global variable in the first place was due to some neat javascript hackery. In some of my tables, i have <a href="http://www.ruby-forum.com/topic/183073#800473">made an entire row clickable </a>, by adding <strong>onclick = &#8220;window.location.href = &#8216;http://whateveriwant.com&#8217; &#8221; </strong>to the <strong>&lt;tr&gt; </strong>element, and setting it&#8217;s style to render a cursor when moused over. I then paired this with rails <strong>url_for</strong> helper to give me a gmail esque feel. The user can click any part of the table row, not just a link, and they will be redirected. Sweet.</p>
<pre>&lt;tr  onclick = "window.location.href =  '&lt;%= url_for :controller =&gt; 'manage', :id =&gt; whymail.id, :type =&gt; "whymail", :action =&gt; 'show' %&gt;' "  style = "cursor:pointer:" &gt;
    &lt;td&gt; My Content &lt;/td&gt;
&lt;/tr&gt;</pre>
<p>While clickable rows is a neat little trick,  what happens when you want to put a button or a checkbox in your table row to really get a gmail feel? When you click your button or your select box, you&#8217;re also clicking in the table row, so you automatically tell the browser to redirect to a different url. Make Sense?</p>
<pre>&lt;tr  onclick = "window.location.href =  '&lt;%= url_for manage_spam_url(id)  %&gt;' "  style = "cursor:pointer:" &gt;
    &lt;td&gt; My Content &lt;/td&gt;
    &lt;td&gt; &lt;%= button_to_remote "Delete", :url =&gt; spam_survey_url(:id =&gt; whymail.id) , :update =&gt; "partial_update" %&gt;&lt;/td&gt;
&lt;/tr&gt;</pre>
<p>So in the above example even if you clicked on the <strong>Delete</strong> button then browser will still redirect to <strong>manage_spam_url </strong>because you still clicked on the table row. Therein lies the rub, and here comes the global javascript variables. To keep the table row redirecting us we can set a variable flag. First we put a <a href="http://en.wikipedia.org/wiki/Ternary_operator">ternary operator </a>in the  table row like this:</p>
<pre>&lt;tr  onclick = "window.location.href = window.myboolean ? '#' : '&lt;%= url_for manage_spam_url(id) %&gt;' "  style = "cursor:pointer:" &gt;</pre>
<p>and we can set our flag in the header like this:</p>
<pre>&lt;script type="text/javascript" charset="utf-8"&gt;
    window.myboolean = false ;
&lt;/script&gt;</pre>
<p>Then in our button we can set the flag to true when it gets pressed:</p>
<pre>&lt;%= button_to_remote "Delete", :url =&gt; spam_survey_url(:id =&gt; whymail.id) , :update =&gt; "partial_update", :loading =&gt; "<strong>window.myboolean = true;</strong>",  :complete =&gt;"<strong>window.myboolean=false;</strong>" %&gt;</pre>
<p>And after the javascript from the button loads we set the flag back to false. Its possible that this is serious over kill. But if anyone finds a better way to do this, let me know. Till then happy global javascripting!!</p>
<pre></pre>
<pre></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.thinkbohemian.com/2009/10/17/global-javascript-variables-and-you/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
