<head>
<%= javascript_include_tag "jquery.js" %>
</head>
<%= link_to 'my blog', "http://railsagainstthemachine.blogspot.com/", :class => 'hello-world' %>
<script>
$(document).ready(
function() {
$(".hello-world").click( function() {
alert("Hello world!");
});
return false;
});
</script>
success: function(data) {
$("#post-detail").html(data);
}<%= link_to 'show post', {:action =>'get'}, {:class => 'ajax_replace', :on_success => "post-detail" } %>
<a href="/jquery_test/get" on_success="post-detail" class="post-detail">show post</a>
// Add ajax behavior to all links which are 'ajax_replace' classesThe problem with this is that jquery behaviours are only bound to dom items when the document ready event fires. So any new items added as a result of the ajax request will have the required behaviour. So we turn to the live query plugin
// the function requests html asyncronsusly and updates the target div
// specified by the 'on_success' attribute of the link
$(function() {
$(".ajax_replace").click( function() {
var on_success = this.getAttribute("on_success");
$.ajax({
url: this.href,
dataType: "html",
beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/html");},
success: function(data) {
$("#"+on_success).html(data);
}
});
return false;
});
// Add ajax behavior to all links which are 'ajax_form' classes
// the function requests html asyncronsusly and updates the target div
// specified by the 'on_success' attribute of the link if no success target is specified
// we assume that we send as javascript
$(".ajax_form").each(
//for each selected element run this code
function( intIndex ){
//Get the target for this element
var on_success = this.getAttribute("on_success");
//setup the parameter hash
var options=getFormOptions(on_success);
//Apply ajax form to this element
$( this ).ajaxForm(options);
});//end of code applied to each element
function getFormOptions(on_success)
{
var options = {
dataType: "html",
beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/html");},
success: function(data) {
$("#"+on_success).html(data);
}
};
return options;
}
});
$(document).ready(function() {
$(".ajax_form").livequery(attach_ajax_form);
$(".ajax_replace").livequery(attach_ajax_link);
});//
// Add ajax behavior to all links which are 'ajax_replace' classes
// the function requests html asyncronsusly and updates the target div
// specified by the 'on_success' attribute of the link
function attach_ajax_link()
{
$(this).click( function() {
var on_success = this.getAttribute("on_success");
$.ajax({
url: this.href,
dataType: "html",
beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/html");},
success: function(data) {
$("#"+on_success).html(data);
}
});
return false;
});
}
// Add ajax behavior to all links which are 'ajax_form' classes
// the function requests html asyncronsusly and updates the target div
// specified by the 'on_success' attribute of the link if no success target is specified
// we assume that we send as javascript
function attach_ajax_form()
{
$( this ).each(
//for each selected element run this code
function( intIndex ){
var on_success = this.getAttribute("on_success");//Get the target for this element
var options=getFormOptions(on_success);//setup the parameter hash
$( this ).ajaxForm(options); //Apply ajax form to this element
});//end of code applied to each element
}
//Builds the form options for ajax form
function getFormOptions(on_success)
{
var options = {
dataType: "html",
beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/html");},
success: function(data) {
$("#"+on_success).html(data);
}
};
return options;
}
July 2007 August 2007 September 2007 December 2007 January 2008 February 2008 March 2008 April 2008 June 2008 July 2008 August 2008 October 2008 November 2008 January 2009
Subscribe to Comments [Atom]