getScript加入本地负载不是全局的?负载、全局、不是、getScript

2023-09-10 19:29:40 作者:烟花再美、也只是一瞬间

从我已阅读使用一个名为全球性的eval功能jQuery的getScript加入脚本函数加载脚本文件在全球范围内。是否有一个特定的设置或方法来改变这种做法,它会直接作用我是从调用它?在加载

如果我未定义为不加载在本地范围内的脚本。下面code名称返回

 函数callscript(){
        VAR名称='弗雷德';
    getScript加入脚本(abc.js);
    }

//abc.js:
警报(名称);
 

解决方案

我相信我已经找到使用普通的JQuery的AJAX调用的解决方案。关键就在于,设置数据类型为文本,否则,如果它的脚本,或者使用getScript加入或替代方案。获得(),它会自动运行脚本中,并将其在全球范围内。

 函数ABC(){
    VAR味精=侨;
    $阿贾克斯({
      网址:'主题/ _default /系统/ message.js',
      成功:功能(数据){
          的eval(数据);
      },
      数据类型:文字
    });
    }
    //message.js
(功能() {
    警报(味精);
})();
 
计算机管理本地用户里没有显示,win7系统计算机管理中没有本地用户和组的解决方法...

此提示侨预期:)

在任何人说任何事是我使用的eval,但其完全没有这种情况。

From what I have read JQuery's getScript function loads the script file in a global context using a function called 'global eval'. Is there a particular setting or method to change this so it will instead load within the function I am calling it from?

If I do the following code name returns undefined as its not loading the script in the local context.

    function callscript(){
        var name='fred';
    getScript(abc.js);
    }

//abc.js:
alert(name);

解决方案

I believe I have found the solution using a regular JQuery ajax call. The trick is you set the datatype to 'text' as otherwise if its script or if use getScript or the alternative .get() it will auto run the script inside and place it in the global context.

 function abc(){
    var msg="ciao";
    $.ajax({
      url: 'themes/_default/system/message.js',
      success: function(data){
          eval(data);
      },
      dataType: "text"
    });
    }
    //message.js
(function() {
    alert(msg);
})();

This alerts 'ciao' as expected :)

Before anyone says anything yes I'm using eval but its perfectly fine in this situation.