JS not running first time HTML page loads

对着背影说爱祢 提交于 2019-12-04 15:15:00

Inside your .get, make sure you are specifying the type of data. By adding your "refresh" to .done, it tells AJAX to wait until after the success call to reload the table, I believe it will work. Another thing you can try, is put the javascript after your </table> in the body so the HTML table is always loaded first.

<script type="text/javascript">
$(function() {
    jQuery.get('filename.txt', function(data) {
    var lines = data.split("\n");  
    var newrow = "";
    for (var i = 0, len = lines.length; i < (len-1); i=i+2) {
        newrow = "<tr><td>"+lines[i]+"</td>"+"<td>"+lines[i+1]+"</td></tr>";   
        $('#tableoverview').find('tbody:last').append(newrow);
        }
    },'text').done(function() {
                $("#tableoverview").table("refresh");
            });
}); 
</script>

You can also try $(document.body).ready which should not execute until after the body has loaded.

<script type="text/javascript">
$(document.body).ready(
    $(function() {
        jQuery.get('filename.txt', function(data) {
        var lines = data.split("\n");  
        var newrow = "";
        for (var i = 0, len = lines.length; i < (len-1); i=i+2) {
            newrow = "<tr><td>"+lines[i]+"</td>"+"<td>"+lines[i+1]+"</td></tr>";   
            $('#tableoverview').find('tbody:last').append(newrow);
            }
        $("#tableoverview").table("refresh");
        });
    });
);
</script>

EDIT:

I did some research on this and you seem to be using a mix of the table widget and the reflow table widget .table("refresh");.
Have you tried removing that and using .table("reflow"); along with <table data-role="table" id="tableoverview" data-mode="reflow" class="ui-responsive table-stroke"> instead?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!