使用bootstrap 3.0模式加载iframe中的动态远程内容

时间:2013-11-19 12:12:23

标签: javascript html5 twitter-bootstrap iframe modal-dialog

我已经尝试了其他问题和stackexchange上发布的一些建议,但没有任何工作令我满意。

我正在尝试将动态内容加载到模态中。具体来说,iFrame中的YouTube视频或Soundcloud音频。因此,访问该网站的任何用户都可以输入视频或音频的链接。然后模态动态加载用户链接。每个后续用户都可以在一个模态中看到彼此的链接。 (为每个用户分别加载模态)

我不能让这个工作得很好。我创建了一个名为“modal.html”的单独html文件来测试它,其中包含一个带有正确的YouTube / Soundcloud剪辑的iframe。

我也很困惑我是否需要在我的标签内使用“data-remote =”,或者href是否足够?或者我在第一个模态中使用数据遥控器。或两者,或者其中之一?两者都没有效果。

这是我的代码:

<a data-toggle="modal" href="modal.html" data-target="#myModal">Click me</a>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" data-     remote="modal.html" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
  </div>
  <div class="modal-body">

  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary">Save changes</button>
  </div>
</div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

5 个答案:

答案 0 :(得分:36)

为什么data-remotehref无法在youtube等远程网站上运行

Twitter bootstrap的模式使用AJAX通过data-remote / href加载远程内容。 AJAX受same origin policy约束,因此访问具有不同来源的网站(如youtube)会产生此错误:

  

请求的资源

上没有'Access-Control-Allow-Origin'标头

因此data-remotehref都不会做你想要的。

<强> JSON : 如果您获得了json数据,那么您可能会使用JSONP。但是因为你需要ht,而不是像youtube这样的网站,我们需要另一种方法:

使用<iFrame>

的解决方案

<iframe>适用于youtube和许多其他远程网站(即使这个搜索也不适用于所有网站,因为有些像Facebook一样,通过将X-Frame-Options'设置为'DENY'来明确阻止iframe )。

将iframe用于动态内容的一种方法是:

1)在模态的正文中添加一个空的iframe:

<div class="modal-body">
    <iframe frameborder="0"></iframe>
</div>

2)添加一些单击模式对话框按钮时触发的jquery。以下代码需要data-src属性中的链接目标,并且该按钮具有类modalButton。您可以选择指定data-widthdata-height - 否则它们分别默认为400和300(当然您可以轻松更改这些)。

然后在<iframe>上设置属性,这会导致iframe加载指定的页面。

$('a.modalButton').on('click', function(e) {
    var src = $(this).attr('data-src');
    var height = $(this).attr('data-height') || 300;
    var width = $(this).attr('data-width') || 400;

    $("#myModal iframe").attr({'src':src,
                               'height': height,
                               'width': width});
});

3)将类和属性添加到模态对话框的锚标记中:

<a class="modalButton" data-toggle="modal" data-src="http://www.youtube.com/embed/Oc8sWN_jNF4?rel=0&wmode=transparent&fs=0" data-height=320 data-width=450 data-target="#myModal">Click me</a>

Demo Fiddle using youtube

答案 1 :(得分:12)

如果您需要显示预先格式化的网页,则需要这样的内容

$('#btn').click(function() {
    $.ajax({
        url: 'url-src/dialog.html',
        dataType: 'json',
        type: 'POST',
        success: function(data) {
            if (data.check) {
                var $modal = $(data.dialog);
                $('body').append($modal);
                $modal.filter('.modal').modal();
            } else {
                alert(data.dialog);
            }

        }
    });

});

dialog.html的HTML

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3>Dialog</h3>
        </div>
        <div class="modal-body">
            <form role="form"  class="form-horizontal">
                <div class="form-group">
                    <label class="col-sm-3 control-label" for="mutual">Example: </label>
                    <div class="col-sm-6">
                        <input type="text" id="example-form" value="" class="form-control">
                    </div>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button id="edit-form"  data-id-mutual="" class="btn btn-primary">Save</button>
            <button class="btn btn-danger" data-dismiss="modal" aria-hidden="true">Cancel</button>
        </div>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

这个html有一个表单示例,你必须在里面添加一个带有视频的html。

答案 2 :(得分:2)

可能是一篇旧帖子,我前段时间遇到过类似的问题,我想按一个链接,将文本文件(或任何其他文件)的href传递给模态窗口内的iframe,我解决了这样:

&#13;
&#13;
function loadiframe(htmlHref) //load iframe
{
document.getElementById('targetiframe').src = htmlHref;
}


function unloadiframe() //just for the kicks of it
{
var frame = document.getElementById("targetiframe"),
frameHTML = frame.contentDocument || frame.contentWindow.document;
frameHTML.removeChild(frameDoc.documentElement);	
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css" rel="stylesheet"/>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>


<a class=" btn btn-danger" onClick="loadiframe('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css')" data-toggle="modal" data-target="#myModal">LINK</a><!--link to iframe-->




<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      
      <div class="modal-header" style="border:hidden">
        <button type="button" class="close" onClick="unloadiframe()" data-dismiss="modal" aria-label="Close"><span aria-   hidden="true">&times;</span></button>
      </div>
      
      <div class="modal-body" style="padding-top:10px; padding-left:5px; padding-right:0px; padding-bottom:0px;">
        
      <iframe src="" frameborder="0" id="targetiframe" style=" height:500px; width:100%;" name="targetframe" allowtransparency="true"></iframe> <!-- target iframe -->
        
      </div> <!--modal-body-->
      
      <div class="modal-footer" style="margin-top:0px;">
        <button type="button" class="btn btn-default pull-right" data-dismiss="modal" onClick="unloadiframe()">close</button>
      </div>
      
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

因此,在这种情况下,您只有一个模态,一个iframe,您可以加载和卸载。

答案 3 :(得分:1)

您可以尝试this bootstrap helper to dialogs

它支持ajax请求,iframe,常用对话框,确认并提示!

您可以将其用作:

eModal.iframe('http://someUrl.com', 'This is a tile for iframe', callbackIfNeeded);

eModal.alert('The message', 'This title');

eModal.ajax('/mypage.html', 'This is a ajax', callbackIfNeeded);

eModal.confirm('the question', 'The title', theMandatoryCallback);

eModal.prompt('Form question', 'This is a ajax', theMandatoryCallback);

这会在加载iframe时提供加载进度!

不需要HTML。

您可以使用对象文字作为额外选项的参数。

查看网站表格更多详情。

最好的,

答案 4 :(得分:0)

function javascript_function(item_code)
{
var d = new Date();
var url_to_zoom='http://localhost.com/application/page.php?id=2&item_code='+item_code+'&reqdate='+d.getTime();

$('#modal_box').modal({keyboard:true,backdrop:true,show:true,remote:url_to_zoom});
}