简体   繁体   English

AJAX内容未使用已加载的Javascript源

[英]AJAX Content not using Loaded Javascript Source

I have a file called incident.php that loads my main page. 我有一个名为events.php的文件,它将加载我的主页。 This includes header, footer and sidebar. 这包括页眉,页脚和侧边栏。 The content of the page I am loading from incident.inc.php. 我要从identity.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. 由于这种情况一直在变化,因此我选择使用AJAX加载此内容并以30秒的间隔重新加载,以使其可以作为仪表板打开。

For the dashboard code I am using Twitter Bootstrap and this dashboard: https://wrapbootstrap.com/theme/metro-admin-template-WB0KDM51J 对于仪表板代码,我正在使用Twitter Bootstrap和以下仪表板: https : //wrapbootstrap.com/theme/metro-admin-template-WB0KDM51J

This dashboard has multiple JS files that it loads at the bottom of each page. 该仪表板在每个页面的底部加载了多个JS文件。 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. 我发现当我通过AJAX加载内容时,它与加载的javascript源文件一起无法正常工作。 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. 我已经看到了多种方法来使脚本在Ajax内容加载时运行,但是还没有看到如何识别仪表板包括的这20多个javascript文件。 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. 我发现的唯一解决方法是将所有这些javascript文件包括在event.inc.php中,我认为这不是正确的方法。

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). 我正在寻找有关如何从AJAX加载内容以利用原始文件(incident.php)中包含的Javascript文件的最佳实践。

My AJAX call within incident.php is as follows. 我在identity.php中的AJAX调用如下。

(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. 我只是无法确切指出在这种情况下该如何做。

You could have incident.inc.php send back two results instead of plain HTML. 您可能让identity.inc.php发回了两个结果,而不是发回纯HTML。

Now it probably responds with something like this: 现在它可能会响应如下:

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

If you respond with JSON: 如果您使用JSON进行响应:

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

You could then have some storage of already loaded javascript files as well, so you wouldn't have to load the same file again. 然后,您还可以存储一些已经加载的javascript文件,因此您不必再次加载相同的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM