Inject me

Manfred Stienstra, 30 Nov 2005, 12:54 in ruby on rails and javascript, last updated 19 Apr 2006, 16:36 (edit).

A month or so ago I found myself staring at my screen, I had done something stupid. Almost the entire application was finished, but when I tested it on another domain nothing worked. When I started exploring the Firefox javascript window and pasted some errors in Google, I found out I couldn’t do cross-domain XMLHttpRequests. I looked at Thijs, my boss / colleague / friend, and said: ‘We have a little problem.’ After some looking around we found out that Julien Lamarre had solved our problem.

In order to fit this solution into our Rails app, I wrote inject.js and some helpers. When plugin support came out I converted my solution to a plugin, and after some testing it is ready for the world. You can download it here.

Some comments are probably in place. The Inject.Request object doesn’t work exactly like the Ajax.Request object as it always evaluates the response from the server and doesn’t support the same arguments. Please take a look at the code, because it’s the best documentation right now.

The general idea behind the javascript is that it creates an invisible div element in the body element of the page and injects script tags into this div. The URL’s in the src attribute of the script tags are actually calls to Rails controllers and actions. You will have to return javascript from the actions if you want to change the state of the page, play nice and return a ‘text/javascript’ content-type when you do this.

Comments

  1. Anon 40 days later: (delete)

    Do you have an updated version of inject for Windows? The plugin caused my Windows ROR server from booting up.

    Also, any examples would be awesome. Where do you store your javascript? Would it be in the view? Or do you read it from an external file in the javascripts directory?

    Thx in advance.

  2. Manfred 40 days later: (delete)

    The plugin is pure ruby, so it should work on any platform. Can you check your logs to see if it throws any exceptions?

    I will try to post a short example this week.

    About the javascript; when you use a piece of javascript on more than one page, it makes sense to put it in an external file so the browser can cache the javascript. External javascript also keeps your pages clean and simple to read. I try to only put javascript in the views when it's absolutely neccesary.

  3. Anon 41 days later: (delete)

    Thx for the quick reply. I am very new to both ruby and rails, so please bear with me.

    No logs are generated. The webrick server simply crashes while booting up.

    Also, in init.rb the only statement is
    require 'inject_helpers'

    and _init.rb seems to be a binary file (MAC OSX)

    Similarly in the javascript directory there is a _inject.js which again seems to be binary.

    Thx in advance.

  4. Manfred 42 days later: (delete)

    You can delete the _* files, those are osx resource forks. I'm not sure why webrick is crashing, are you sure it's caused by the plugin?

Add your comment

In order to fight spam on this blog, posting comments from a browser without javascript is currently not supported.