Search code examples
jqueryjquery-uiiframebrowser-history

jQuery UI Dialog Iframe - Remove From Browser History Upon Close


I use a jQuery UI Dialog with an iframe in order to provide an in-page "pop-up" for editing. Every time the dialog is opened, it adds two requests to the browser to the history: one for the URL loaded when opened, and one for setting the src to blank when the dialog is closed. If the end-user clicks the back button, they don't see anything happening, because it's going back in the iframe that is no longer being displayed.

So, I understand why this is happening, but I'm having trouble figuring out how to prevent it. Is there a way to "rewind" the history on the iframe when it is closed?

$(document).ready(function() {
    var $iframe = $('<iframe id="uiDialogIframe" data-reload="0" frameborder="0" marginwidth="0" marginheight="0" width="800" height="450" />');
    var $dialog = $('<div id="uiDialogIframeWrapper" />').append($iframe).appendTo('body').dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        width: 'auto',
        height: 'auto',
        close: function () {
            $iframe.attr('src', '');
            if ('1' === $iframe.attr('data-reload')) {
                location.reload();
            }
        }
    });
    $('a.uiDialogIframe').on('click', function (e) {
        e.preventDefault();
        var $this = $(this);
        $iframe.attr('src', $this.attr('href'));
        $dialog.dialog('option', 'title', $this.data('title'))
                .dialog('open');
    });
});

Solution

  • I ended up using avoiding the creation of history in the first place, by recreating the iframe every time.

    Parent Script

    $(document).ready(function() {
        var $dialog = $('<div id="uiDialogIframeWrapper" data-reload="0" />').appendTo('body').dialog({
            autoOpen: false,
            modal: true,
            resizable: false,
            width: 'auto',
            height: 'auto',
            close: function () {
                // check for reload flag
                if ('1' === $dialog.attr('data-reload')) {
                    location.reload();
                }
            }
        });
        $dialog.on('goto', function(e, url) {
            $dialog.empty().append('<iframe frameborder="0" marginwidth="0" marginheight="0" width="800" height="450" src="' + url + '" />');
        });
        $('a.uiDialogIframe').on('click', function (e) {
            e.preventDefault();
            var $this = $(this);
            $dialog.trigger('goto', $this.attr('href'))
                    .dialog('option', 'title', $this.data('title'))
                    .dialog('open');
        });
    });
    

    Child Script

    $(document).ready(function() {
        // get the dialog object
        var $dialog = parent.jQuery('#uiDialogIframeWrapper');
        // flag the parent to reload
        $('.parentReload').on('click', function() {
            $dialog.attr('data-reload', '1');
        });
        // close the dialog
        if (0 < $('body.closeDialog').length) {
            $dialog.dialog('close');
        }
        // use goto for internal links
        $('a[href^="/"]').on('click', function(e) {
            e.preventDefault();
            $dialog.trigger('goto', $(this).attr('href'));
        });
    });