【问题标题】:Ajax and JS filesAjax 和 JS 文件
【发布时间】:2016-07-07 08:48:21
【问题描述】:

我在进行 ajax 调用时遇到问题。每次我想在ajax控制的区域刷新或添加更多数据时,我都必须包含一些js文件。

这是我提交数据的常用脚本

<script type="text/javascript">
    function ajax_loader_order(x, y) {
        $.getScript("js/plugins.js");
        $.getScript("js/scripts.js");
        req = $.ajax({
            type: "GET",
            url: "products.php?order=" + y + "&orderud=" + x,
            datatype: "html",
            success: function(data){
                $('#container').html(data);
                $('html, body').animate({scrollTop: $("#main").offset().top}, 1000);
            }
        });
    }
    </script>

每次加载这些js文件,会使网站变得更重和更慢。

有没有一种方法可以让我在第一次加载时加载这些 js 文件,并且每次我调用这个 ajax 函数时都可以包含在内?

提前谢谢你

【问题讨论】:

  • 将那些JS文件添加到你的head标签中
  • 我已经这样做了,但没有成功。
  • 为什么每次调用 ajax 时都需要包含这些脚本?
  • 把那些 js 文件放在

标签: javascript ajax file


【解决方案1】:

在您的 html 中导入 ajax 脚本后添加它们

伪代码:

<html>
  <head>
    <script src="ajax.js" />
    <script src="js1.js" />
    <script src="js2.js" />
  </head>
  <body>
  </body>
</html>

或:

<html>
  <head>
  </head>
  <body>
    <script src="ajax.js" />
    <script src="js1.js" />
    <script src="js2.js" />
  </body>
</html>

【讨论】:

  • 这个ajax脚本必须执行多次,它是用来分页的。所以我每次都要包含那些js文件。
  • 是的,它必须执行多次,但只加载一次
猜你喜欢
  • 2015-10-22
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多