views:

907

answers:

1

The jquery load method loads HTML from a remote file and injects it into the DOM. For example, to load the feeds.html file into the div with the ID of feeds, you would do this:

$("#feeds").load("feeds.html");

Is this an alternative to calling a partial with the Rails replace_html method or is the functionality slightly different?

page.replace_html 'feeds', :partial => 'main/feeds',
    :locals => {:feed =>  @feed_data }

[EDIT]: As Craig Stuntz points out, replace_html returns Javascript instead of HTML - what's the advantage/disadvantage of this? Does it just mean that the fragment you return has more functional capabilities in the same way that a web page that uses Javascript is more powerful than a regular HTML page? Or is there some other reason for returning Javascript instead of HTML?

+5  A: 

The end result is much the same, but they work in a very different way. The load method means that your server returns an HTML fragment, and jQuery inserts it into the page. The replace_html method means that your server is returning JavaScript instead of HTML, and this JavaScript is executed to insert an HTML fragment into the page. So the final resulting HTML is the same, but the traffic between the client and the server is very different.

Craig Stuntz
What's the advantage/disadvantage of returning Javascript instead of HTML? Does it just mean that the fragment you return has more functional capabilities in the same way that a web page that uses Javascript is more powerful than a regular HTML page? Or is there some other reason for returning Javascript instead of HTML?
pez_dispenser
I think it really comes down to whether you care about what is sent over the wire or not. If the only client who ever fetches your URI is your own Rails app, then it might not matter at all. If, on the other hand, other clients might fetch your URI, perhaps with a client which is not a browser, then it might matter a great deal. It really depends upon what you are doing.
Craig Stuntz
Do you mean because some clients won't be able to process Javascript but would more than likely be able to process HTML? I thought HTML and JS were more or less technological siblings such that any client that can process one can more than likely process the other also?
pez_dispenser
Some clients choose not to handle JavaScript because it can be a security hole. Both Adobe Reader and Apple QuickTime, for example, have had serious security problems due to their JavaScript handling.
Craig Stuntz