简体   繁体   English

如何在JavaScript中有条件地包含脚本文件?

[英]How do I conditionally include a script file in JavaScript?

I want to know if there is a way to conditionally include a script file inside of <script></script> tags. 我想知道是否有办法在<script></script>标记内有条件地包含脚本文件。

I'm trying to do something like this: 我正在尝试做这样的事情:

<script>
if(condition)
{
    <script src="/hts/functions.js"></script> //include this file if condition is met
}
</script>

I found this code: 我发现此代码:

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});

However, I don't understand how to use it. 但是,我不知道如何使用它。

Is there a way to do this? 有没有办法做到这一点?

You're on the right track with $.getScript: 您在$ .getScript的正确轨道上:

if(someConditionIsTrue){
    // load script from the server
    $.getScript( "somePath/onTheServer/script.js", function( data, textStatus, jqxhr ) {
        // do something after the load is complete
    });
}

To use the script, add the code in the .getScript callback to your JavaScript for the page. 要使用脚本,请将.getScript回调中的代码添加到页面的JavaScript中。 Any code that depends on the script you are injecting should be included or called from inside the .getScript callback. 应该在.getScript回调内部包含或调用取决于您要注入的脚本的任何代码。

In the code you pasted, the console.log statements run after the injected script loads. 在您粘贴的代码中, console.log语句在注入的脚本加载后运行。 Here's an example of using $.getScript to load the Underscore.js library and log the version of Underscore to the console: 这是一个使用$.getScript加载$.getScript库并将$.getScript版本记录到控制台的示例:

var condition=true;
var pathToScript='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js';
if(condition){
    // load script from the server
    $.getScript( pathToScript, function( data, textStatus, jqxhr ) {
        console.log(_.VERSION);
    });
};

JSFiddle: http://jsfiddle.net/WA4f4/2/ JSFiddle: http : //jsfiddle.net/WA4f4/2/

I slightly modified your code so it would work in JSFiddle and to demonstrate that the code inside the callback to getScript can access variables from the injected script. 我稍加修改了您的代码,使其可以在JSFiddle中运行,并演示了getScript回调中的代码可以访问注入脚本中的变量。

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

相关问题 如何在Greasemonkey脚本中包含远程javascript文件? - How do I include a remote javascript file in a Greasemonkey script? 如何有条件地包含远程JavaScript文件? - How to include a remote JavaScript file conditionally? 如何在 Angular 中包含 JavaScript 脚本文件并从该脚本调用函数? - How do I include a JavaScript script file in Angular and call a function from that script? 如何在include.js文件中包含jQuery脚本? - how do I include a jQuery script into an include.js file? 如何在另一个 JavaScript 文件中包含一个 JavaScript 文件? - How do I include a JavaScript file in another JavaScript file? 作为更大的 Ruby on Rails 项目的一部分,我如何有条件地在 html.slim 文件中包含一些 Javascript? - How can I conditionally include some Javascript in an html.slim file, as part of a larger Ruby on Rails project? 如何在 HTML Google Apps 脚本中包含文件? - How do I include a file in HTML Google Apps Script? 如何在React中的某些HTML中间有条件地包含标记 - How do I conditionally include a tag in the middle of some HTML in React 如何在引导程序中包含外部javascript文件 - how do I include an external javascript file with bootstrap 如何在Angular中有条件地包含脚本元素 - How to conditionally include script element in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM