Javascript 正在寻找获取js/css解决方案

Javascript 正在寻找获取js/css解决方案,javascript,internet-explorer,promise,fetch,Javascript,Internet Explorer,Promise,Fetch,我正在寻找一个类似或的解决方案。我非常喜欢fetchinjec,因为我可以注入多个文件,甚至css。唯一的问题是,它不支持IE10,这对我来说很不幸。有什么想法和建议吗?哦,也没有jquery 在fetch inject中,其工作原理如下: fetchInject([ 'assets/vendor/foo.js', 'assets/vendor/bar.js', 'assets/vendor/foo.css' ]).then(() => { // after

我正在寻找一个类似或的解决方案。我非常喜欢fetchinjec,因为我可以注入多个文件,甚至css。唯一的问题是,它不支持IE10,这对我来说很不幸。有什么想法和建议吗?哦,也没有jquery

在fetch inject中,其工作原理如下:

fetchInject([
    'assets/vendor/foo.js',
    'assets/vendor/bar.js',
    'assets/vendor/foo.css'
]).then(() => {
    // after load, do something        
});
fetch inject的主要代码是:

const fetchInject = function (inputs, promise) {
if (!arguments.length) return Promise.reject(new ReferenceError("Failed to execute 'fetchInject': 1 argument required but only 0 present."))
if (arguments[0] && arguments[0].constructor !== Array) return Promise.reject(new TypeError("Failed to execute 'fetchInject': argument 1 must be of type 'Array'."))
if (arguments[1] && arguments[1].constructor !== Promise) return Promise.reject(new TypeError("Failed to execute 'fetchInject': argument 2 must be of type 'Promise'."))

const resources = [];
const deferreds = promise ? [].concat(promise) : [];
const thenables = [];

inputs.forEach(input => deferreds.push(
  window.fetch(input).then(res => {
    return [res.clone().text(), res.blob()]
  }).then(promises => {
    return Promise.all(promises).then(resolved => {
      resources.push({ text: resolved[0], blob: resolved[1] });
    })
  })
));

return Promise.all(deferreds).then(() => {
  resources.forEach(resource => {
    thenables.push({ then: resolve => {
      resource.blob.type.includes('text/css')
        ? head(window, document, 'style', resource, resolve)
        : head(window, document, 'script', resource, resolve);
    } });
  });
  return Promise.all(thenables)
})

})

这是一种方法。缺点是您不知道文件何时加载,好处是它在任何地方都可以工作,并且不会干扰其他代码:

function addScript(script) {
    var jsElement = document.createElement("script");
    jsElement.type = "application/javascript";
    jsElement.src = script;
    document.body.appendChild(jsElement);
}

function addScript("YOUR-SCRIPT-URL.js");

这是一种方法。缺点是您不知道文件何时加载,好处是它在任何地方都可以工作,并且不会干扰其他代码:

function addScript(script) {
    var jsElement = document.createElement("script");
    jsElement.type = "application/javascript";
    jsElement.src = script;
    document.body.appendChild(jsElement);
}

function addScript("YOUR-SCRIPT-URL.js");

似乎我需要添加一个不同的polyfill,它叫做whatwgfetch。它包含导致IE出现问题的
Response.clone()
。如果有人想在IE10+中使用fetch inject,请使用此选项,并且字符串也包含polyfill。这些将涵盖所有情况。

似乎我需要添加一个不同的polyfill,它称为whatwg fetch。它包含导致IE出现问题的
Response.clone()
。如果有人想在IE10+中使用fetch inject,请使用此选项,并且字符串也包含polyfill。这些将涵盖所有情况。

您的意思是IE10不支持fetch?请解释为什么需要支持IE10。每台可以运行IE10的计算机都可以运行IE11。事实上,每台可以运行IE11的计算机也可以运行基于Chromium的新的Microsoft Edge浏览器。IE10作为目标的合法场景很少。@evolutionxbox IE10没有,但IE11也没有-但您可以使用围绕XHR的包装对其进行多填充(但对于
承诺
,您还需要单独的多填充),例如,为什么不使用webpack之类的构建时JS系统,或者在HTML中预呈现
元素?有很多事情你没有告诉我们,还有很多其他的解决方案。@Dai谢谢你的意思是IE10不支持fetch?请解释为什么你需要支持IE10。每台可以运行IE10的计算机都可以运行IE11。事实上,每台可以运行IE11的计算机也可以运行基于Chromium的新的Microsoft Edge浏览器。IE10作为目标的合法场景很少。@evolutionxbox IE10没有,但IE11也没有-但您可以使用围绕XHR的包装对其进行多填充(但对于
承诺
,您还需要单独的多填充),例如,为什么不使用webpack之类的构建时JS系统,或者在HTML中预呈现
元素?有很多事情你没有告诉我们,还有很多其他的解决方案。@Dai感谢你发布这个问题的解决方案。您可以在48小时后将您的答案标记为可接受的答案,此时可进行标记。它可以在将来帮助其他社区成员解决类似的问题。感谢您的理解。感谢您发布此问题的解决方案。您可以在48小时后将您的答案标记为可接受的答案,此时可进行标记。它可以在将来帮助其他社区成员解决类似的问题。谢谢你的理解。