Say you have an onClick event in a link to change a different image on the page. Your code should be:
<a href="javascript:void(0)" onClick="changeImage(); return false;">Change Image</a>
The return false will make the image always display in IE6.
<a href="javascript:void(0)" onClick="changeImage(); return false;">Change Image</a>
# these are just some constants. Most likely you'll want to find things # in your database to return. TYPES = {"car" => [["Honda Accord", "1"], ["Scion Tc", "2"], ["Ford Focus", "3"], ["BMW M6", "4"]], "suv" => [["Jeep Wrangler", "5"], ["Ford Explorer", "6"], ["Toyota Highlander", "7"]], "truck" => [["Ford F150", "8"], ["Dodge Ram", "9"]] } # this is the action to get the page for the first time. def show_list @types_all = [["Car", "car"], ["SUV", "suv"], ["Truck", "truck"]] end # this is the action that the AJAX request will call. types[] will be # specified in the parameters list, containing the different vehicle # types that the user has selected. def update_list @vehicles_all = [] if params[:types] params[:types].each do |t| @vehicles_all = @vehicles_all.concat(TYPES[t]) end end # this part is really important, if you don't tell it not to render the # layout, the area where your selection box is will have the whole # controller's layout around it. render :layout => false end
<% # this is a function I wrote in my application controller to load a
# Javascript file. show_list.js must be loaded.
add_javascript_file("show_list", false) -%>
<% # I don't actually have anything to respond to this form, it's just to show
# select boxes.
form_tag({}, {:name => "show_list", :id => "show_list"}) do -%>
<p>Vehicle Types:
<%= select_tag "types[]", options_for_select(@types_all, []),
{:size => 3, :multiple => true, :id => "types[]"} %>
</p>
<% # the observe_field must call a function and not directly call a method
# on the controller. Select form elements with multiple elements do not
# get correctly passed as parameters to the controller action, so we
# have to manually do it in a Javascript function.
# with any other type of field (including single selects), you can
# specify :url as a parameter and it will call that url.
# if :frequency is not present, a request will be made every time the
# selection is changed. This isn't recommended, because the user may
# very quickly change selections. 0.5 ensures that a request is made
# at most every half second while the user is changing the selection.
-%>
<%= observe_field "types[]",
:function => "typeChanged('vehicle_list', $F('types[]'))",
:frequency => 0.5 %>
<% # the hourglass will show while the browser is waiting for a response
# from the server, to let the user know something is going on.
-%>
<div style="display:none; text-align:center;" id="hourglass">
<%= image_tag "ani-busy.gif", {:width => 32, :height => 32} %>
</div>
<p>Vehicles from selected types:
<% # the whole select tag must be in a div. This is what will be updated.
# If the browser is Firefox, you can put the select tag ID as the area
# to update in the AJAX call, and return a list of options in your rhtml,
# but this will NOT work in Internet Explorer.
-%>
<div id="vehicle_list">
<%= select_tag "vehicles[]", "", {:size => 9, :multiple => true} %>
</div>
</p>
<% end -%><% # render just the select tag here
-%>
<%= select_tag "vehicles[]", options_for_select(@vehicles_all, []),
{:size => 9, :multiple => true} %>function typeChanged(areaToUpdate, typesToSend) {
// Manually create a request string with parameters
var queryStr = "/node/update_list";
var started = false;
if (typesToSend) {
for (var i = 0; i < typesToSend.length; i++) {
// the first type must have ? before it to start the parameters list.
// after that, it should be prepended with & to specify an additional
// parameter.
queryStr += (started ? "&" : "?") + "types[]=" + typesToSend[i];
started = true;
}
}
// before we send the request, show the hourglass.
$('hourglass').show();
// manually send an Ajax request to update the area passed in to the
// function. Upon completion, hide the hourglass. You can also specify
// functions to run for onError, onSuccess, and many others.
new Ajax.Updater(areaToUpdate, queryStr,
{onComplete: function() { $('hourglass').hide(); } });
}require 'socket' # this is the address to bind to. The first argument is the port, the second # is the IP address (a string). Pass 0 for the port to not bind to a specific # port (the port will be auto assigned by the OS when a connection is made). # The address MUST be an actual external address of the computer - 127.0.0.1 # won't work. in_addr = Socket.pack_sockaddr_in(12345, "10.10.1.3") # for the outbound address, you must specify both a port and address out_addr = Socket.pack_sockaddr_in(80, "www.google.com") s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) s.bind(in_addr) s.connect(out_addr) # from this point you can read from and write to using the standard Socket # and IO methods. s.close