Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Modal Remote Source Error Handling

We are using Bootstrap Modal window to display some html that is loaded via a remote source. We're doing this via the recommended way in the Bootstrap docs, by using the option remote and passing it a url. (As described here)

For example:

$('#id').modal({remote:'index.html'});

My question: Is it possible to handle an error in the case that index.html is not available?

I don't see any answer in the documentation.

I know this should rarely happen, however if someone has a slow or spotty connection, I'd rather show them an error than to just hang with an empty modal.

like image 486
nostromo Avatar asked Aug 27 '13 03:08

nostromo


People also ask

How do you prevent bootstrap modal from hiding when clicked outside the content area?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.

How do I stop a bootstrap modal from closing?

If you want to prevent boostrap modal from closing by those actions, you need to change the value of backdrop and keyboard configuration. The default value of backdrop and keyboard is set to true . You can change the configuration in HTML or Javascript directly.

How do I stop bootstrap modal pop up?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

How check bootstrap modal is open or not?

How check modal is open or not in jQuery? You can simply use the modal('show') method to show or open the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('hide') and modal('toggle') .


1 Answers

Currently the Github repo ( /js/modal.js ) contains this fragment in the modal plugin definition:

…
if (this.options.remote) this.$element.load(this.options.remote)
…

Which indicates that no callback is used, the result of the request is directly assigned to the dom element being worked on.

From the docs jQuery.load:

This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data.

Later in the doc there is a code snippt that describes how to detect a failure with load:

$("#success").load("/not-here.php", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});

It seems the Twitter team opted to not handle the error.

Maybe it's time to start an issue, it seems like a "mobile first" library would want to handle this kind of thing gracefully ;-) https://github.com/twbs/bootstrap/issues

like image 172
Mark Fox Avatar answered Oct 07 '22 14:10

Mark Fox