1

I'm using this code to load twitter's web intent javascript functionalities only and only if the share modal is opened:

$(function(){
    $('#share').on('show.bs.modal', function(e){
        if(typeof(twttr) === 'undefined') {
            $.getScript('https://platform.twitter.com/widgets.js');
        }
    });
});

It works great. Now, I also want to completely remove the show.bs.modal event from the modal once it has been triggered for the first time, because it will be completely unnecessary afterwards.

I already tried:

$('#share').off('show.bs.modal');

but I'm guessing that since it's not a standard event, it won't work like that. Also, I've searched for event removal methods in the bootstrap docs, but it doesn't seem to be anything like that in there.

Any ideas? Thanks!

Andres SK
  • 10,779
  • 25
  • 90
  • 152
  • https://stackoverflow.com/questions/14451052/in-twitter-bootstrap-how-do-i-unbind-an-event-from-the-closing-of-a-modal-dialo – Amanjot Kaur Jan 15 '20 at 05:33
  • @AmanjotKaur that won't work. 1) unbind() is deprecated, jQuery offers .off() instead. 2) `$('#share').off('show.bs.modal');` doesn't work. – Andres SK Jan 15 '20 at 05:39

2 Answers2

0

I guess it's all about event type of modal window.

Probably you meant shown.bs.modal instead of show.bs.modal.

$(function(){
    $('#share').on('shown.bs.modal', function(e){
        if(typeof(twttr) === 'undefined') {
            $.getScript('https://platform.twitter.com/widgets.js');
        }
    });
});

$('#share').off('shown.bs.modal');
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0
$('#share').on('shown.bs.modal', function(e){
    $('#share').off('shown.bs.modal');
    $.getScript('https://platform.twitter.com/widgets.js');
});
Plamen S.
  • 1
  • 1
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Jan 05 '23 at 20:07