Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a Bootstrap Modal automatically from an external link

I have a page with 4-5 different modals attached to it. Each with a unique #value (obv). I am trying to find a way to link to these individual modals' from an external source (in this case an HTML email).

For example:

"Click here to view directions" in the email needs to link to -->> www.myurl.com/#directions

the #directions piece should trigger the : function on page load and have the modal automatically open.

Is this possible or am i only dreaming?

like image 689
stankobrin Avatar asked Dec 09 '22 11:12

stankobrin


1 Answers

You can use the hastag to decide the content to show. For example,

JS

$(document).ready(function () {
    var target = document.location.hash.replace("#", "");
    if (target.length) {
        if(target=="option1"){
          showModal("title1","content1");
        }
        else if(target=="option2"){
            showModal("title2","content2");
        }
    }else{
        showModal("no title","no content");
    }

    function showModal(title,content){
        $('#myModal .modal-title').html(title);
        $('#myModal .modal-body').html(content);
        $('#myModal').modal('show');
    }

});

HTML - Bootstrap modal code

<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">&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>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

this will show content for option1

http://jsfiddle.net/fFcS2/show/#option1

this will show content for option2

http://jsfiddle.net/fFcS2/show/#option2

the code

http://jsfiddle.net/fFcS2

like image 64
melc Avatar answered Dec 11 '22 01:12

melc