How does Rails RJS (AJAX) work?

In javascript, the way to execute code fetched via ajax is use XHR,
attach a callback function, and eval the response text.

But in rails I see none of that (yes I use firebug), and when responding
asynchronously, whatever in the rjs file gets executed by the browser
automatically. I am quite confused.

Does the browser execute automatically any http response that has
content-type: text/javascript in its header? Or are there other stuff
going on.

Max Y. wrote in post #1004874:

In javascript, the way to execute code fetched via ajax is use XHR,
attach a callback function, and eval the response text.

But in rails I see none of that (yes I use firebug), and when responding
asynchronously, whatever in the rjs file gets executed by the browser
automatically. I am quite confused.

Does the browser execute automatically any http response that has
content-type: text/javascript in its header? Or are there other stuff
going on.

In a word… Yes.

However, this is not drastically different than any other JavaScript
executed by the browser. Typically there is a <script src="…"
…> in the HTML HEAD that tells the browser to download the
JavaScript file referenced, which gets executed immediately. This is why
it’s important to wait for DOM loaded or Page loaded events for any
script that accesses, or modifies the DOM.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs