Twitter bootstrap modal external urls are not working

前端 未结 3 1093
[愿得一人]
[愿得一人] 2020-12-14 12:10

Twitter bootstrap modal doesn\'t load external urls inside modal.

Sample Code : jsFiddle



        
相关标签:
3条回答
  • 2020-12-14 12:43

    This doesn't work because you are violating the same origin policy restriction that prevents you from sending cross domain AJAX requests which is what bootstrap is using here. So one possibility is to load the external content into an <iframe>:

    $('a.btn').on('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        $('.modal-body').html('<iframe width="100%" height="100%" frameborder="0" scrolling="no" allowtransparency="true" src="' + url + '"></iframe>');
    });​
    

    Be warned though that some sites (such as google, stackoverflow, facebook, ...) cannot be loaded into an iframe. They are setting the X-Frame-Options response HTTP header to SAMEORIGIN and also check if they are loaded inside an iframe.

    You can see it in action in this fiddle: http://jsfiddle.net/f2Fcd/

    0 讨论(0)
  • 2020-12-14 12:58

    Try this:

    <script type="text/javascript" charset="utf-8">
    $(function() {
     $('*[data-modal]').click(function(e) {
     e.preventDefault();
     var href = $(e.target).attr('href');
      if (href.indexOf('#') == 0) {
       $(href).modal('show');
      } else {
        $.get(href, function(data) {
        $('<div class="modal">' + data + '</div>').modal('show').appendTo('body');
                            });
                        }
                    });
                });
            </script>
    
    0 讨论(0)
  • 2020-12-14 12:59

    This snippet helps in fetching the modal content via Ajax:

    $.get(href, function(response) {
         $("#myModal .modal-content").html(response);
    });
    

    Keep in mind, that the HTML response fetched, must containg the dom-elements normally present inside the "modal-content" element:

     <div class="modal-header">HEADER</div>
     <div class="modal-body">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">Do It</button>
     </div>
    
    0 讨论(0)
提交回复
热议问题