Skip to content

Instantly share code, notes, and snippets.

@diddipoeler
Forked from neiljohnston/Bootstap 3.1.1 Modal Fix
Created December 13, 2015 08:38
Show Gist options
  • Save diddipoeler/10a78904315e7827af19 to your computer and use it in GitHub Desktop.
Save diddipoeler/10a78904315e7827af19 to your computer and use it in GitHub Desktop.
Bootstrap 3.1.1 Modal Remote Loading Fix - Simulate BS2 & 3 Modal Behaviour
As of Bootstrap 3.1.1 modal behaviour has changed. When you load remote content into the modal, the revised behaviour sees the content loaded into the modal's root. This destroys the modal's internal layout, meaning that the remote content will need to be modified to mimic the layout. For many user's that will be impractical.
To reinstate the old behaviour we're going to hijack Bootstrap 3.1.1's modal behaviour a hair. This will be done using Bootstrap 3's documented modal mark up, so the behaviour should be seemless.
I'm going to keep the code variable heavy and descriptive so those new to Javascript can follow along. One of the bonuses of Bootstrap is that it democratizes mobile first and responsive development to new coders. I'm also assuming the presence of JQuery. Javascript heavy lifter types will handily streamline the code.
For reference here's a link that invokes a BS3 modal:
<li><a data-toggle="modal" href="terms.html" data-target="#terms">Terms of Use</a></li>
In youre Javascript you're going to need the following.
// Make sure the DOM elements are loaded and accounted for
$(document).ready(function() {
// Match to Bootstraps data-toggle for the modal
// and attach an onclick event handler
$('a[data-toggle="modal"]').on('click', function(e) {
// From the clicked element, get the data-target arrtibute
// which BS3 uses to determine the target modal
var target_modal = $(e.currentTarget).data('target');
// also get the remote content's URL
var remote_content = e.currentTarget.href;
// Find the target modal in the DOM
var modal = $(target_modal);
// Find the modal's <div class="modal-body"> so we can populate it
var modalBody = $(target_modal + ' .modal-body');
// Capture BS3's show.bs.modal which is fires
// immediately when, you guessed it, the show instance method
// for the modal is called
modal.on('show.bs.modal', function () {
// use your remote content URL to load the modal body
modalBody.load(remote_content);
}).modal();
// and show the modal
// Now return a false (negating the link action) to prevent Bootstrap's JS 3.1.1
// from throwing a 'preventDefault' error due to us overriding the anchor usage.
return false;
});
}); // close off document ready
We're just about there. One thing you may want to do is style the modal body with a
max-height, so that long content will scroll.
In your CSS, you'll need the following:
.modal-body{
max-height: 300px;
overflow-y: scroll;
}
Just for refference I'll include the modal's HTML, which is a knock-off of every Bootsrap Modal Example you've ever seen:
<div id="terms" class="modal fade">
<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>
<h3 id="termsLabel" class="modal-title">TERMS AND CONDITIONS</h3>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Cheers,
Neil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment