Bootstrap has created a series of tools that it wants to make as extensible as possible. This means, that they want to allow the developer using their tools to "listen" to a number of "Events" and perform follow up actions.
The two listed in your question allow the developer to "hook" into either the "show" event or "hide" event. (There are actually 2 more provided by bootstrap ["shown" and "hidden"]). From the Documentation these events are fired when:
show.bs.tab
This event fires on tab show, but before the new tab has been shown. Use event.target
and event.relatedTarget
to target the active tab and the previous active tab (if available) respectively.
shown.bs.tab
This event fires on tab show after a tab has been shown. Use event.target
and event.relatedTarget
to target the active tab and the previous active tab (if available) respectively.
hide.bs.tab
This event fires when a new tab is to be shown (and thus the previous active tab is to be hidden). Use event.target
and event.relatedTarget
to target the current active tab and the new soon-to-be-active tab, respectively.
hidden.bs.tab
This event fires after a new tab is shown (and thus the previous active tab is hidden). Use event.target and event.relatedTarget to target the previous active tab and the new active tab, respectively.
An example of how this might be used could be the case of "When the user clicks on a tab, stop playing the youtube video in the previous tab by refreshing its src". The code for something like this could be
$('#myTabs').on('show.bs.tab', function(event){
// previous tab
if(event.relatedTarget){
var youtubeIframe = $(event.relatedTarget).find('iframe');
var src = youtubeIframe.attr('src');
youtubeIframe.attr('src', 'about:blank');
youtubeIframe.attr('src', src);
}
});
Alternatively, you could target the tab that was just "hidden". Rather than looking to the current tab and using its relatedTarget
we would use the hide.bs.tab
event and use event.target
, like so:
$('#myTabs').on('hide.bs.tab', function(event){
// current tab
if(event.target){
var youtubeIframe = $(event.target).find('iframe');
var src = youtubeIframe.attr('src');
youtubeIframe.attr('src', 'about:blank');
youtubeIframe.attr('src', src);
}
});
NB: Im sure there are better ways to stop playing youtube videos. This is used as example code.