如何获得.js文件上下文jQuery $.接收时不执行Ajax

how to get .js file context with jQuery $.ajax without execute when receive

本文关键字:执行 Ajax js 何获得 文件 上下文 jQuery      更新时间:2023-09-26

获取*.js文件收到后执行脚本!

如何在我想要的时候获得和执行?和如何在我想要的时候销毁这个脚本?

不确定是否使用jQuery,但可以使用XHR方法。

function getScript(b, c){
  var a = new XMLHttpRequest();
  a.open('get', b, true);
  a.onreadystatechange = function(){
    a.readyState != 4 || a.status && a.status != 200 || c(a.responseText);
  };
  a.send();
}
getScript('script.js', function(content){
    // Do whatever here
    Function(content)(); // Execute the script
});

我找到答案了!

在$.ajax上使用'converters'例如:

$.ajax({
    url: "test.js",
    converters: {
        'text script': function (text) {
            return text;
        }
    },
    success: function(scripts) {
        //do something
    }
});