<?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>Wanderwort by Roman Ernst &#187; jQuery</title>
	<atom:link href="http://wanderwort.de/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://wanderwort.de</link>
	<description>Interkulturelle Wortkolletionen in Ruby, PHP, XHTML, CSS nah am Strom der Gesellschaft</description>
	<lastBuildDate>Mon, 14 Mar 2011 15:10:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Cross-Domain with Sinatra / Rack and JSONP</title>
		<link>http://wanderwort.de/2010/03/25/sinatra-rack-with-jsonp/</link>
		<comments>http://wanderwort.de/2010/03/25/sinatra-rack-with-jsonp/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 00:45:35 +0000</pubDate>
		<dc:creator>pex</dc:creator>
				<category><![CDATA[Gemachtes]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://wanderwort.de/?p=284</guid>
		<description><![CDATA[There are several situations one might want to do cross-domain (/cross-site) AJAX-requests. Probably you experience the security borders of modern browsers very fast. JSONP is a hack for JSON that provides a great workaround. It means sending a callback with the URL (eg. ?callback=sampleFunction). Server-sided we setup our application to respond with a JSON body [...]]]></description>
			<content:encoded><![CDATA[<p>There are several situations one might want to do cross-domain (/cross-site) <a href="http://de.wikipedia.org/wiki/Ajax_(Programmierung)">AJAX</a>-requests. Probably you experience the security borders of modern browsers very fast.<br />
<a href="http://en.wikipedia.org/wiki/JSONP#JSONP">JSONP</a> is a hack for <a href="http://www.json.org/">JSON</a> that provides a great workaround. It means sending a callback with the URL (eg. ?callback=sampleFunction). Server-sided we setup our application to respond with a JSON body wrapped in a function named like our callback function. In this case</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">sampleFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;ourData&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;is an array&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>   
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h3>What we have to do (Sample)</h3>
<p><b>Frontend</b><br />
JS-Frameworks like <a href="http://jquery.com/">jQuery</a> are smart enough to send such requests with ease. Lets write a simple function with jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'get'</span><span style="color: #339933;">,</span>
  url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'file.json'</span><span style="color: #339933;">,</span>
  dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jsonp'</span><span style="color: #339933;">,</span>
  success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doSomethingWith<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><b>Let&#8217;s see how to setup the server:</b><br />
Using <a href="http://www.ruby-lang.org/en/">Ruby</a> and <a href="http://rack.rubyforge.org/">Rack</a> it becomes very simple to beautifully provide regular JSON for non-JSONP clients and JSONP for jQuery &#038; Co.<br />
Simply use the rack middleware <a href="http://github.com/rack/rack-contrib/">rack-contrib by Ryan Tomayko</a></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> rack-rack-contrib <span style="color: #660033;">--source</span>=http:<span style="color: #000000; font-weight: bold;">//</span>gems.github.com<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>If you&#8217;re using pure rack, put this in your config.ru</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rack/contrib/jsonp'</span>
use <span style="color:#6666ff; font-weight:bold;">Rack::JSONP</span></pre></div></div>

<p>Using <a href="http://www.sinatrarb.com/">Sinatra</a>, I&#8217;d put it somewhere with my config files.<br />
Let&#8217;s see how a sample <a href="http://www.sinatrarb.com/">Sinatra</a> application could look like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'json'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rack/contrib/jsonp'</span>
&nbsp;
use <span style="color:#6666ff; font-weight:bold;">Rack::JSONP</span>
&nbsp;
before <span style="color:#9966CC; font-weight:bold;">do</span>
  content_type <span style="color:#ff3333; font-weight:bold;">:json</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
get <span style="color:#996600;">'/posts'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  Posts.<span style="color:#9900CC;">find</span>.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">to_json</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That simple, this powerful.</p>
]]></content:encoded>
			<wfw:commentRss>http://wanderwort.de/2010/03/25/sinatra-rack-with-jsonp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>select any HTML text in element with jQuery</title>
		<link>http://wanderwort.de/2009/11/19/select-any-html-text-in-element-with-jquery/</link>
		<comments>http://wanderwort.de/2009/11/19/select-any-html-text-in-element-with-jquery/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 15:25:44 +0000</pubDate>
		<dc:creator>pex</dc:creator>
				<category><![CDATA[Gemachtes]]></category>
		<category><![CDATA[Arbeit]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://wanderwort.de/?p=170</guid>
		<description><![CDATA[The following script jQuery extension selects any text from a given jQuery selector. Tested with Firefox and Safari &#8211; should work in IE6+ as well. jQuery.fn.extend&#40;&#123; selectText: function&#40;&#41; &#123; var text = $&#40;this&#41;&#91;0&#93;; if &#40;$.browser.msie&#41; &#123; var range = document.body.createTextRange&#40;&#41;; range.moveToElementText&#40;text&#41;; range.select&#40;&#41;; &#125; else if &#40;$.browser.mozilla &#124;&#124; $.browser.opera&#41; &#123; var selection = window.getSelection&#40;&#41;; var range [...]]]></description>
			<content:encoded><![CDATA[<p>The following <del datetime="2009-11-19T15:26:12+00:00">script</del> <ins datetime="2009-11-19T15:26:37+00:00">jQuery extension</ins> selects any text from a given jQuery selector. Tested with Firefox and Safari &#8211; should work in IE6+ as well.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  selectText<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: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">browser</span>.<span style="color: #660066;">msie</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> range <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">createTextRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      range.<span style="color: #660066;">moveToElementText</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      range.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">browser</span>.<span style="color: #660066;">mozilla</span> <span style="color: #339933;">||</span> $.<span style="color: #660066;">browser</span>.<span style="color: #660066;">opera</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> selection <span style="color: #339933;">=</span> window.<span style="color: #660066;">getSelection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> range <span style="color: #339933;">=</span> document.<span style="color: #660066;">createRange</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      range.<span style="color: #660066;">selectNodeContents</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      selection.<span style="color: #660066;">removeAllRanges</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      selection.<span style="color: #660066;">addRange</span><span style="color: #009900;">&#40;</span>range<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">browser</span>.<span style="color: #660066;">safari</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> selection <span style="color: #339933;">=</span> window.<span style="color: #660066;">getSelection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      selection.<span style="color: #660066;">setBaseAndExtent</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> text<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>To use it simly do:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> shortSelector <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#name'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">selectText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> longSelector <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul#names li:contains(&quot;Hampel&quot;)'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">selectText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>written on top of <a href="http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse">Source</a> and <a href="http://www.codingforums.com/archive/index.php/t-105808.html">Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wanderwort.de/2009/11/19/select-any-html-text-in-element-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery and stopPropagation</title>
		<link>http://wanderwort.de/2009/01/12/jquery-and-stoppropagation/</link>
		<comments>http://wanderwort.de/2009/01/12/jquery-and-stoppropagation/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 15:01:58 +0000</pubDate>
		<dc:creator>pex</dc:creator>
				<category><![CDATA[Gemachtes]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://wanderwort.de/?p=40</guid>
		<description><![CDATA[Often you might not even notice the following problem: Imagine you got a table row or a list element including several links. By default you defined a function like $&#40;'table tr'&#41;.click&#40;function&#40;event&#41; &#123; target = $&#40;this&#41;.find&#40;'a:first'&#41;.attr&#40;'href'&#41;; open&#40;target, '_self'&#41;; &#125;&#41; In this case the function finds the first defined link in our row and links it to [...]]]></description>
			<content:encoded><![CDATA[<p>Often you might not even notice the following problem: Imagine you got a table row or a list element including several links. By default you defined a function like</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table tr'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a:first'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'_self'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>In this case the function finds the first defined link in our row and links it to the row itself.<br />
If we click on the link, we get redirected to the defined target. But as the link is lying in our linked row, this click will also be executed and our target is loaded twice. Even though the user won&#8217;t notice it, functions like deleting or answering will throw errors.<br />
jQuery got a very nice core function to suppress the second loading. </p>
<p><a href="http://docs.jquery.com/Types/Event#event.stopPropagation.28.29">stopPropagation()</a></p>
<p>We are using this function to stop the propagation of our click-event like shown:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table tr'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a:first'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'_self'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table tr a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  event.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>It reminds me of a &#8220;return false;&#8221;. Just look for the bind function here:<br />
 <a href="http://jquery.bassistance.de/api-browser/">jQuery API Browser</a>. There you may find more details on similiar cases, too.</p>
<p>UPDATE for Internet Explorer</p>
<p>As this won&#8217;t work in MS&#8217; IE, you have to use the similar</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">event.<span style="color: #660066;">cancelBubble</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>(<a href="http://msdn.microsoft.com/de-de/library/system.windows.browser.htmleventargs.stoppropagation(VS.95).aspx">Source</a>)</p>
<p>If you put both behind each other, IE would still throw some errors. So you should try following if-function if you are using jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    agent <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">browser</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>agent.<span style="color: #660066;">msie</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		event.<span style="color: #660066;">cancelBubble</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		event.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://wanderwort.de/2009/01/12/jquery-and-stoppropagation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

