Amazon.com Widgets
...not so private reflections of greg.newman
Categories & Search

Recently in javascript Category

A fantastic prototype script for Mac dictionary lookups.


var selection;
if(window.getSelection)
  selection = window.getSelection();
else if(document.selection)
  selection = document.selection.createRange();

document.observe('dblclick', function() {
  if(navigator.userAgent.include('Macintosh')) {
      location.href = 'dict://' + selection;
    }
});

A quick (and probably dirty) Prototype-based hack allowing Mac users to get the definition of any word by double clicking it.

[Via AlternateIdea - Home.]

EJS via MinusMor

Feb 17, 2007

In a recent project I had to come up with a way to include a partial based on a condition from a dropdown selection. Conditionals in RJS? I don't think so. So thank you Dan Webb for your kick-ass MinusMor plugin. What this does is allow you to embed ruby in javascript as an .ejs extension which gets evaluated before any rjs templates.

So with an observe_field I do:

<%= observe_field 'field_to_evaluate', :url => {:action => 'my_ejs_template_name' } %>

I create an ejs template and insert the following code.

if (document.getElementById('field_to_evaluate').value == "my_true_value") { $('div_container_to_update').update(<%= partial('the_partial_to_show') %>); }

Now the field it is observing is a dropdown menu. Whenever the selection is true, I see my partial.
Again, a big thanks to Dan Webb!

Happy Programmer's Day

Sep 13, 2006

Rails Ajax Wiki

Jul 10, 2006

While checking my Mint stats this morning I ran across a link to LesserWiki, a Ajaxified Ruby on Rails Wiki that looks promising.

I noticed right away that the wiki's page seems to keep a read history appending the latest article to the top of the page. The downside; there is no back button so until you realize what it's doing it there is a little bit of confusion.

I'm curious if anyone has used it in a production environment and if so how does it perform.

A very simple way to keep machines out of your forms is through CAPTCHA's. A very simple implementation of this in your Rails forms is by using a nifty little javascript. This is not the preferred method, since there are ways around the javascript, but it is definitely a nice little spam helper.
Note: This will not stop spiders, only human interaction with forms. This is only intended to show how to integrate jcap with a rails app. Not in anyway a tutorial on how to stop spiders

CAPTCHA

Set it up

1. Download the Jcap scripts from here. Decompress and copy them to your public/javascripts folder. In my case, I left the cimg folder within my javascripts folder. 2. Add the javascript include tags to your view (within the head).
<%= javascript_include_tag 'jcap', 'md5' %>
3. Add an onload event to the body tag.
<body onload="document.xfrm.uword.focus();">
4. Modify your form tags to resemble:
<%= form_tag({:action => 'create'}, {:onSubmit => 'return jcap()', :name => 'xfrm'}) %> <%= render :partial => 'addlistingform' %> <noscript>[This resource requires a Javascript enabled browser.]</noscript> Please enter the code as it is displayed above. <input type=text name="uword" id="uword" value="" size=20> <%= submit_tag "Create" %> <%= end_form_tag %>

I modified my jcap.js file to reflect the location of my cimg folder. If you keep yours in the javascripts folder you will need to do so as well.

The developer of the Jcap scripts recommends you use some further steps to make sure your captcha is covered by requiring the verification form field.

This is a neat script but does not solve all problems. As I mentioned, if a browser does not have javascript enabled this will fail.