I'm creating a jQuery dialog that can be opened and closed from many different buttons, everything is working fine, except when I close the dialog, the browser scrolls to the top. I've tried many of the suggestions online, but none seem to keep the functionality working while preventing the scroll.
I'm creating the dialog like so:
function create_hint_widget(hint_text) {
if ($("#hint_dialog").hasClass("ui-dialog-content"))
{
$("#hint_dialog").dialog("open");
$("#hint_dialog").html(hint_text);
}
else
{
return $("<div id='hint_dialog' title='Help'>" + hint_text + "</div>")
.dialog({
resizable: true,
height: 200,
width: 300,
modal: false,
position: ['right', 180],
close: function(ev, ui)
{
$("#hint_dialog").dialog("close");
},
});
}
}
This is the Rails code to create the link calling the dialog:
<%= link_to image_tag("hint_link.jpeg", :size=>"13x13"),
'javascript:;', :onclick => 'create_hint_widget("Enter a name for your template. Each template must have a unique name.");
return false;' %>
I'm using the open/close for the dialog because I want it to retain its position. event.stopPropagation(); doesn't stop the scroll and event.preventDefault(); breaks the open/close functionality. Probably most important, when I remove my close: function, it no longer scrolls on close, but some of the open/close functionality stops performing.