Warm tip: This article is reproduced from serverfault.com, please click

Bootstrap popover works on second load

发布于 2020-11-27 19:42:30

I have made a webpage on Sharepoint with bootstrap and the content is populated with javascript. I am using popover in a table. The table is generated via javascript. The strange thing is that the popover works only after I made a refresh of the page/reloaded it with F5.

A popover outside of the table works fine and by first load. As well as the code runs fine without problems on my local machine, but on sharepoint it breaks down.

Here some code - initialization:

<script src="jquery-3.5.1.slim.min.js"></script>
<script src="popper.min.js"></script>
<script src="bootstrap.min.js"></script>

then the function for generating the table is called:

<body onload="javascript: GenerateTable();">

followed by the popper call:

$(function () {
  $('.example-popover').popover({
  })
})

The result is a table which contains the following line with the popper:

<td>Here is a question which needs a popper as info!&nbsp;&nbsp;
<div class="row justify-content-end">       
<a href="#!" tabindex="0" class="badge badge-info" role="button" data-toggle="popover" data-trigger="focus" title="" data-content="This is a funny popover" title="Info">Info</a>
</div>
</td>

It seems to me like it an issue with the loading order - but can't figure out why it works locally but not on Sharepoint.

Questioner
kingbrain
Viewed
0
kingbrain 2020-12-01 14:28:08

The problem was the order of the javascript execution. As the code is loaded from an external javascript file the order how the code is loaded is not known.

Thus it is recommended to put the javascript function from the html file into an explicit function into the javascript file. Then the function has to be called explicitly.

Javascript File:

function PopperCall(){
$('.example-popover').popover({});
$('.popover-dismiss').popover({trigger: 'focus'});
$('[data-toggle="popover"]').popover();
}

In the HTML the loading is done by:

<body onload="javascript: GenerateTable(); PopperCall();">