Trigger method without changing url

Hi, I’d like to know how can I do a button that on click with call an
action, but wont reload the page.

Like the “like” button at facebook.

Thanks in advance.

Rodrigo R. wrote in post #1049716:

Hi, I’d like to know how can I do a button that on click with call an
action, but wont reload the page.

If you’re calling back to your same origin (domain) then you can use
XHTTPRequest (a.k.a. AJAX). We almost never use that object directly.
It’s usually managed by a JavaScript framework such as jQuery.

Example:

$(function() {
$(‘my_button’).click(clickHandler);
});

function clickHandler() {
$(‘target_div’).load(‘http://example.com/posts’);
}

This would first wait for the DOM to load (to make sure the button
element is available), then bind a click event to the button element.

Clicking the button calls the handler function, which then uses jQuery
to send a request to a URL that returns an HTML fragment, which finally
gets loaded inside of the target_div.

Like the “like” button at facebook.

I’m guessing the Facebook Like button has to do some fairly tricky
things to get around the “same origin” policy. It doesn’t sound like
that’s really what you need.

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