How to display an IFRAME inside a jQuery UI dialog

前端 未结 3 657
盖世英雄少女心
盖世英雄少女心 2020-11-28 04:40

The web application that I am upgrading uses jQuery and jQuery UI. I have replaced most instances of window.open and with j

相关标签:
3条回答
  • 2020-11-28 05:03

    The problems were:

    1. iframe content comes from another domain
    2. iframe dimensions need to be adjusted for each video

    The solution based on omerkirk's answer involves:

    • Creating an iframe element
    • Creating a dialog with autoOpen: false, width: "auto", height: "auto"
    • Specifying iframe source, width and height before opening the dialog

    Here is a rough outline of code:

    HTML

    <div class="thumb">
        <a href="http://jsfiddle.net/yBNVr/show/"   data-title="Std 4:3 ratio video" data-width="512" data-height="384"><img src="http://dummyimage.com/120x90/000/f00&text=Std+4-3+ratio+video" /></a></li>
        <a href="http://jsfiddle.net/yBNVr/1/show/" data-title="HD 16:9 ratio video" data-width="512" data-height="288"><img src="http://dummyimage.com/120x90/000/f00&text=HD+16-9+ratio+video" /></a></li>
    </div>
    

    jQuery

    $(function () {
        var iframe = $('<iframe frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>');
        var dialog = $("<div></div>").append(iframe).appendTo("body").dialog({
            autoOpen: false,
            modal: true,
            resizable: false,
            width: "auto",
            height: "auto",
            close: function () {
                iframe.attr("src", "");
            }
        });
        $(".thumb a").on("click", function (e) {
            e.preventDefault();
            var src = $(this).attr("href");
            var title = $(this).attr("data-title");
            var width = $(this).attr("data-width");
            var height = $(this).attr("data-height");
            iframe.attr({
                width: +width,
                height: +height,
                src: src
            });
            dialog.dialog("option", "title", title).dialog("open");
        });
    });
    

    Demo here and code here. And another example along similar lines

    0 讨论(0)
  • 2020-11-28 05:04

    There are multiple ways you can do this but I am not sure which one is the best practice. The first approach is you can append an iFrame in the dialog container on the fly with your given link:

    $("#dialog").append($("<iframe />").attr("src", "your link")).dialog({dialogoptions});
    

    Another would be to load the content of your external link into the dialog container using ajax.

    $("#dialog").load("yourajaxhandleraddress.htm").dialog({dialogoptions});
    

    Both works fine but depends on the external content.

    0 讨论(0)
  • 2020-11-28 05:09

    Although this is a old post, I have spent 3 hours to fix my issue and I think this might help someone in future.

    Here is my jquery-dialog hack to show html content inside an <iframe> :

    let modalProperties = {autoOpen: true, width: 900, height: 600, modal: true, title: 'Modal Title'};
    let modalHtmlContent = '<div>My Content First div</div><div>My Content Second div</div>';
    
    // create wrapper iframe
    let wrapperIframe = $('<iframe src="" frameborder="0" style="width:100%; height:100%;"></iframe>');
    
    // create jquery dialog by a 'div' with 'iframe' appended
    $("<div></div>").append(wrapperIframe).dialog(modalProperties);
    
    // insert html content to iframe 'body'
    let wrapperIframeDocument = wrapperIframe[0].contentDocument;
    let wrapperIframeBody = $('body', wrapperIframeDocument);
    wrapperIframeBody.html(modalHtmlContent);
    

    jsfiddle demo

    0 讨论(0)
提交回复
热议问题