Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open remote content in the Bootstrap modal window

Tags:

All I need is a simple example in how to open a remote content into a Twitter Bootstrap modal window.

I'm using Bootstrap v2.0.4 and I just can't get it to work. I can open regular modal windows, but I can't make it open a remote file inside it.

like image 694
fackz Avatar asked Aug 28 '12 02:08

fackz


People also ask

How do I open a Bootstrap modal window?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

What is modal content in Bootstrap?

Modals are built with HTML, CSS, and JavaScript. They're positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead. Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

How do I make Bootstrap modal open by default?

Answer: Use the Bootstrap . modal('show') method modal('show') method for launching the modal window automatically when page load without clicking anything.


1 Answers

First, remote data must fulfill the same origin policy. If you're not satisfying that, everything after this is going to fail (if you are trying to load external URL's, see Twitter Bootstrap external URL's are not working).

There are two ways to accomplish loading remote content into a modal with the Bootstrap data-api. Namely to either specify the remote url to load in the <a> element which triggers the modal or to specify the url in the modal's markup.

In the former, one uses the href property:

<a data-target="#myModal" href="/remote/url" role="button" class="btn" data-toggle="modal">Launch demo modal</a> 

In the latter, one uses the data-remote property:

<div class="modal fade hide" id="myModal" data-remote="/remote/url">...</div> 

The advantage of specifying it in the <a> element is that one could have a different remote url for each <a>, yet still only use a single modal. Using the data-remote property might be more advantageous in situations where you have multiple means of launching a modal with the same content. Then, no matter what launches it (even a JS call), it would consistently provide the same remote content, without having to duplicate that information across all the methods of invocation.

Here is a demo using the latter method:

JSFiddle

The content in the body of the modal is the remote html.

like image 84
merv Avatar answered Nov 02 '22 04:11

merv