0

I have a file called incident.php that loads my main page. This includes header, footer and sidebar. The content of the page I am loading from incident.inc.php. As this is ever changing I have opted to use AJAX to load this content and reload at 30 second intervals allowing it to be left open as a dashboard.

For the dashboard code I am using Twitter Bootstrap and this dashboard: https://wrapbootstrap.com/theme/metro-admin-template-WB0KDM51J

This dashboard has multiple JS files that it loads at the bottom of each page. I have found that when I am loading the content through AJAX that it doesn't behave properly along with the loaded javascript source files. I have seen multiple ways of getting a script to run at the time the Ajax content loads, but have not seen how I can get it to recognize these 20 or so javascript files that the dashboard includes. The only work around I have found is to include all of these javascript files within the incident.inc.php which I assume is not the proper way to do it.

I am looking for the best practice on how to have content loaded from AJAX to utilize the included Javascript files in the originating file (incident.php).

My AJAX call within incident.php is as follows.

(function($){
        $.ajaxSetup(
            {
                cache: false,
                beforeSend: function() {
                    $('#incidents').hide();
                    $('#loading').show();
                },
                complete: function() {
                    $('#loading').hide();
                    $('#incidents').show();
                },
                success: function() {
                    $('#loading').hide();
                    $('#incidents').show();
                }
            });
        var $container = $("#incidents");
        $container.load("incident.inc.php");
        var refreshId = setInterval(function()
        {
            $container.load('incident.inc.php');
        }, 30000);
    })(jQuery);

I know that there has to be a better way to do this. I just can't seem to pinpoint exactly how to do it in this situation.

4

1 回答 1

0

您可以让 event.inc.php 发回两个结果而不是纯 HTML。

现在它可能会这样回应:

...
<div>HTML here</div>
...

如果您使用 JSON 响应:

{html:"<div>HTML here...</div>", scripts:["/js/movieplayer.js","/js/myscript.js"]}

然后,您还可以存储一些已加载的 javascript 文件,因此您不必再次加载相同的文件。

于 2013-05-01T02:45:37.270 回答