确定要动态加载 html 中的 js 文件

发布于 2024-11-02 06:38:38 字数 257 浏览 3 评论 0原文

我正在尝试建立一个由 htmls 和 javascript/ajax - jquery 以及许多 jquery 插件组成的项目。

对于许多html页面中的每一个,我都必须使用RequireJS,这就是我想要的..

我想根据属性(ajax调用的返回值)确定我应该加载缩小文件或非缩小文件

所以我喜欢需要一种机制来确定和决定(在页面加载之前或页面开始加载后)我想要加载哪些js文件......然后继续加载。

有没有一个好的方法来做到这一点?

I am trying to set up a project which consist of htmls and javascript/ajax - jquery with many jquery plugins.

For each of many html pages, I have to use RequireJS and here is what i want..

I want to determine based on the property(return value from an ajax call) that I should load the minified files or non minified files

So I kind of need a mechanism to determine and decide (before the page load or as soon as the page starts loading) which js files I would want to load... and then go ahead with the load.

Is there a nice way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

节枝 2024-11-09 06:38:38

好吧,

看一下这个例子:

html

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <!-- data-main attribute tells require.js to load
             scripts/main.js after require.js loads. -->
        <script data-main="scripts/main" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>

script/main.js

$.ajax({
  url: "http://server.com/fetchMeSomeData"
  context: document.body,
  success: function(data){
    //this would be your sample callback
    // you could be fetching the parameter to ask if it is debug state or not.
    var isDebugSuffix = (data.isDebug)? "" : ".min";

    require(["helper/util" + isDebugSuffix], function() {
        //This function is called when scripts/helper/util.min.js is loaded but when the 
        //isDebug would be True, it would load scripts/helper/utils.js

        //your other code can go here ...    
    });

  }
});

希望这能让你走上正路......

well,

take a look at this example:

the html

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <!-- data-main attribute tells require.js to load
             scripts/main.js after require.js loads. -->
        <script data-main="scripts/main" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>

scripts/main.js

$.ajax({
  url: "http://server.com/fetchMeSomeData"
  context: document.body,
  success: function(data){
    //this would be your sample callback
    // you could be fetching the parameter to ask if it is debug state or not.
    var isDebugSuffix = (data.isDebug)? "" : ".min";

    require(["helper/util" + isDebugSuffix], function() {
        //This function is called when scripts/helper/util.min.js is loaded but when the 
        //isDebug would be True, it would load scripts/helper/utils.js

        //your other code can go here ...    
    });

  }
});

hope this set you on your way...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文