1

I'm loading a Share Tweet button inside a Bootstrap Modal. This is working perfectly fine on Safari and Chrome but it's not on Firefox.

https://jsfiddle.net/z4r41638/

This is the html:

<a href="#" onClick="$('#myModal').modal('show')">Modal</a>

<!-- modal privacy -->
<!-- Modal -->
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                 <h4 class="modal-title text-center" id="myModalLabel">My Modal</h4>

            </div>
            <div class="modal-body">
                <div id="containerTweet"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

This is the js:

window.twttr = (function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0],
        t = window.twttr || {};
    if (d.getElementById(id)) return t;
    js = d.createElement(s);
    js.id = id;
    js.src = "https://platform.twitter.com/widgets.js";
    fjs.parentNode.insertBefore(js, fjs);

    t._e = [];
    t.ready = function (f) {
        t._e.push(f);
    };

    return t;
}(document, "script", "twitter-wjs"));

twttr.ready(function (twttr) {
    twttr.widgets.createShareButton(
        'http://getbootstrap.com',
    document.getElementById('containerTweet'), {
        text: "Tweet this!!",
        size: 'large'
    }).then(function (el) {
        console.log('Tweet button added.');
    });
});

Any suggestions?

Carlo S
  • 953
  • 1
  • 9
  • 14

1 Answers1

1

Load the widgets.js script on "shown.bs.modal" event. Use jQuery .one() method so the script is only loaded once on the first time the modal dialog is shown.

$('#myModal').one('shown.bs.modal', function() {
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');
});
icaru12
  • 1,522
  • 16
  • 21