[BlogEntry] Returning and Executing JavaScript from a Rails URL


AuctionRelay.com has been running in Tomcat for a few years. A simple "include" script tag is all it takes to make AuctionRelay add HTML to the middle of an existing web page. The trick is in using the document.write command to make executable JavaScript write visible HTML at the location in the document where it executes.

I'm now in the process of rewriting AuctionRelay to run on Ruby on Rails (shouldn't that really be Rails on Ruby?). This morning, it was time to tackle the issue of setting the HTTP Content-Type header in the response, so that the browser would recognize that the response was executable JavaScript, and not displayable HTML. (Of course, the executable JavaScript is responsible for squirting in the displayable HTML, but that's the kind of convoluted world we've created for ourselves.)

I can browse to http://localhost:3000/js/test, which includes the line

<script src="http://localhost:3000/js/free?id=option_insight"/>

and see something like this:
in the midst of the original HTML page. (What you see here is still the output of the Java version.)


class JsController < ApplicationController

  def out
    @sellerId = @request.params['id'].to_s
    $mylog.debug("free request for " + @sellerId)
    if $freeSellers[@sellerId].nil?
      $mylog.debug("Not found - instantiating...")
      $freeSellers[@sellerId] = Ebay.new
      $mylog.debug("Already exists.")
    @headers["Content-Type"] = "text/javascript"

  def test


<script src="http://localhost:3000/js/out?id=option_insight"/>


document.write('<table border=1 align=center><caption><br/>My eBay feedback rating is\
<a href="http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback&userid=<%= @sellerId %>" target="_blank">\
<%= $freeSellers[@sellerId].feedback %>\
<td colspan=4 align=center>Display your live auctions on any web site for free at\
<a href="http://www.auctionrelay.com" target="_blank">AuctionRelay.com</a></table>');

One of the things I learned the hard way is that the string you pass to document.write must either be on a single line or use backslashes for continuations. (I was trying to make out.rhtml readable. Duh. Hindsight is 20/15 here.) Fortunately, the Firefox JavaScript Console gave me an error message that made me realize what was going on. Unfortunately, it took me 15 minutes of head-scratching to even think to look at it and recognize that the problem was with the actual JavaScript syntax and not something server-side.

[BlogEntry] I'll be with you in 1frdtydangy


Microsoft Office Outlook 2003. A product so advanced that it understands time coordinates from other galaxii.

When this ingenious product pops up a reminder for you, did you know that you can snooze it, not only with the drop-down list of options from minutes to weeks, but that you can also type in an arbitrary floating point amount of time to snooze? You can type in 1m, 5.3 d, 2w, etc. (One problem is that I'm not sure if 1m means I'll be reminded again in a minute or a month. But that's my fault for not RTFM.) But I discovered today that you can also type in 1frdtydangy.

Interestingly, it gave me an error when I tried frdtydangy. Naturally. How can you expect it to delay based on a lone unit of measure, if you don't tell it how many of that unit of measure you mean? I mean, it's not like Outlook 2003 can read your mind or something, right? No, it wasn't until I added a leading 1 that Outlook 2003 understood that 1frdtydangy meant I wanted this particular reminder to snooze for the amount of time it takes the moon of wklcphhhftzsh-po to complete one full revolution around it's neutron star.

I hope it's less than a frdtydangy before I can install OS X on my laptop.

[Comment] Re: Iterating over a Hash in Ruby

Yep, cool. Thanks, Tony!
irb(main):043:0> h1.each{|k,v| puts "#{k} ---> #{v}"}
a ---> 1
b ---> 2
c ---> 3
=> {"a"=>1, "b"=>2, "c"=>3}

This reminds me of something else that has been bugging me since I saw it. What does this syntax mean?

def list
  @category_pages, @categories = paginate :category, :per_page => 10, :order_by => 'category'

How can Ruby return two values from the paginate method?!

[Comment] Re: Bill Maher takes a beating

That's what I was going to write on my blog! Jesus H. Christ does that guy get on your last nerve, or what? Not that some of the talking heads on the right aren't just as annoying, but, at least they're on my side – they trust the president – they know the definition of the word enemy – and they love this country. Great Blog!