3

I want to run jQuery in a Ruby application and use it to manipulate an HTML string. (Yes, I know there are other Ruby libraries that can handle that task, but I have a good reason for using jQuery in this case.) Is this possible? I know that I can use ExecJS to execute simple JavaScript code in my Ruby application, but jQuery doesn't seem to be working:

irb(main):001:0> require 'execjs'
=> true
irb(main):002:0> require 'open-uri'
=> true
irb(main):003:0> context = ExecJS.compile(open("http://code.jquery.com/jquery-1.9.1.js").read)
=> <Return omitted for brevity>
irb(main):004:0> context.call("$", "<div>Test</div>")
ExecJS::ProgramError: ReferenceError: window is not defined
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs/external_runtime.rb:68:in `extract_result'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs/external_runtime.rb:28:in `block in exec'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/execjs-.4.0/lib/execjs/external_runtime.rb:41:in `compile_to_tempfile'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/execjs-.4.0/lib/execjs/external_runtime.rb:27:in `exec'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/execjs-.4.0/lib/execjs/external_runtime.rb:19:in `eval'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/execjs-.4.0/lib/execjs/external_runtime.rb:33:in `call'
        from (irb):4
        from c:/RailsInstaller/Ruby1.9.3/bin/irb:12:in `<main>'

How do I get this to work?

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
  • Ajedi I'm trying to do something similar and asked this question: [Sharing business logic between server-side and client-side of web application?](http://programmers.stackexchange.com/questions/202567/sharing-business-logic-between-server-side-and-client-side-of-web-application/202586?noredirect=1#202586) Let me know what you found out! – thoughtpunch Jun 24 '13 at 17:54
  • @thoughtpunch Unfortunately I haven't taken the time to get much further on this one. What I *want* to do is create a sort of MVC library that runs both client and server-side on a Rails application, so I can take advantage of lightning-fast AJAX-based page loads and dynamic JavaScript animations when the user has JavaScript enabled but still have everything work when they don't (or when Googlebot comes along). I have a few ideas on how I might be able to get this working but nothing solid just yet... – Ajedi32 Jun 24 '13 at 18:19

2 Answers2

4

The error message tells you what the problem is:

ExecJS::ProgramError: ReferenceError: window is not defined

jQuery is a library for DOM manipulation. It expects to see all of the usual DOM objects and interfaces, including the window object.

The ExecJS library you're using doesn't provide a DOM or a window object. It provides a JavaScript execution context, but not the same one a browser provides.

You may possibly be able to get this working by using another library such as JSDOM to provide a DOM that jQuery can use. Here is a search that may give some tips.

The first link describes a use of JSDOM and jQuery with ExecJS that may be similar to what you want.

That said, why jQuery? There are several good Ruby HTML parsers including Nokogiri. Why not try one of those?

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • 1
    Thanks, this is just what I'm looking for. The reason I want to use jQuery is pretty complicated. Basically, I need an interface compatible with [opal-jquery](https://github.com/opal/opal-jquery) so that I can use the same code to manipulate the DOM both client and server-side. That't the idea anyway. – Ajedi32 Mar 15 '13 at 11:50
-4

You can't run jQuery in Ruby. jQuery isn't written in Ruby, it's in JavaScript so you'll need to run a JavaScript interpreter to use it.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303