How to prevent jquery.ui.dialog from scrolling window to the top (when called programaticallly)
Asked Answered
G

5

6

I have the following code:

    $(".foo-form").submit(function (event) {
        event.stopPropagation();
        event.preventDefault();
        $.ajax({
            url: this.action,
            data: $(this).serializeArray(),
            type: 'POST',
            dataType: 'json',
            success: function (data, msg, resp) {
                var $form = $("#second-form");
                $form.show().dialog({
                    resizable: false,
                    height:400,
                    width: 600,
                    modal: true,
                    title: "Recommendation added",
                    buttons: [
                        {
                            text: "OK",
                            click: doOK
                        },
                        {
                            text: "Cancel",
                            click: doCancel
                        }
                    ]
                });
            }
        })
        return false;
    });

If I am scrolled down on the page and submit the form, when the dialog box is displayed it scrolls the page to the top. Is there any way to override this?

Things that are not the solution

  • fixing the positioning for the .ui-dialog class. It's unmodified (using Google's CDN)
  • not canceling out events - as you can see, I call stopPropagation, preventDefault, and return false. So it's not that the event is going through (and even if it were, it's not a hash link to the top of the page anyway)

Using jQuery 1.72 and jQuery UI 1.8.21 (latest versions of each).

Guideboard answered 8/8, 2012 at 21:43 Comment(2)
Try getting its scrollTop and setting the page's scrollTop.Cavetto
You mean something like currentScroll = $(window).scrollTop(); $dialog....{ open: function () { $(window).scrollTop(currentScroll)}} I mean, obviously I can do that. I'd much, much rather just have it not scroll at all, since it's breaking the page.Guideboard
R
6

I had the same issue using jQuery dialog with href tags and I fix it adding "event.preventDefault();" when I invoque the dialog, example:

$(".button").click(function(event){
event.preventDefault();
$("#dialog").dialog();
});
Running answered 16/9, 2013 at 17:35 Comment(2)
If you read my attached code you'll see I use event.stopPropagation, event.preventDefault and return false.Guideboard
This approach worked for my situation, where I had a modal dialog being summoned on a page where the vertical height was longer than the browser (Firefox 23) viewport.Rostock
L
3

I had similar issue with using jQuery dialog with href tags. All I had to do to fix it is to remove the href="#" from the a tag and that solved my issue.

Change<a href='#'>SHOW</a> to <a>SHOW</a>

Lightly answered 10/2, 2013 at 1:23 Comment(0)
G
0

try adding a function for close and use preventDefault inside, worked for me

$form.show().dialog({
                close: function (event) { event.preventDefault(); }
                resizable: false,
                etc....
            });
Gifu answered 9/9, 2014 at 15:12 Comment(0)
M
0

the best fix for this i find is to return false; after you open the dialog.

so just move your return false; outside of the dialog function. I know this is an old question, but didn't see this resolution here and it worked for me.

The issue with your stopPropagation, preventDefault(), and return false; is that they're all in the wrong place.

This is how I do it.

   $("<div><p>" + text + "</p></div>").dialog({
        modal: true,
        resizable: false,
        width: width,
        buttons: {
            Ok: function() {
                $(this).dialog("close");
            }
        }
    });
    return false;
Meletius answered 3/2, 2015 at 13:53 Comment(1)
Either I'm really misunderstanding something, or you're not looking at my code carefully. The stopPropagation and preventDefaults are being called on the form submit event. There is no event in the success function and thus no event to stop. Same with the return false: there is no event on the success function so returning false would have no effect whatsoever.Guideboard
C
-1

It scrolls because of the <a href=""></a> tag.

Use

<a class="someClass" href=#!"></a>

To prevent scrolling to top

Choochoo answered 15/7, 2016 at 12:57 Comment(1)
It's not a link. There is no <a> tag.Guideboard

© 2022 - 2024 — McMap. All rights reserved.