简体   繁体   English

使用 Pace.js 跟踪动态加载的脚本

[英]Use Pace.js to track dynamically loaded script

I am using pjax standalone and pace.js to show page load progress.我正在使用 pjax standalone 和 pace.js 来显示页面加载进度。 All working well.一切正常。

However I have one script I'm loading dynamically following a button click that's pretty large (houndify-web-sdk.min.js 700kb) and would like to show the progress of this load also.但是,我有一个脚本,我在单击一个非常大的按钮(houndify-web-sdk.min.js 700kb)后动态加载,并且还想显示此加载的进度。

The script is currently being added like this:该脚本目前正在添加如下:

var newScript = document.createElement("script");

newScript.setAttribute("src", "/js/large-script.min.js");
document.body.appendChild(newScript);

Is it possible to get pace to track this load?是否有可能加快跟踪此负载的速度?

I have also tried the below Pace.track function without any luck.我也尝试了下面的 Pace.track 功能,但没有任何运气。

Pace.track(function() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "http://large.min.js", true);
  xhr.responseType = "text";

  xhr.onload = function() {
    if (this.status === 200) {
      console.log("loaded");
    // not sure how I would then put the response into a script src file
    }
  };
  xhr.send();
});

Any help/advice would be very much appreciated!任何帮助/建议将不胜感激!

If anyone else is having trouble with this - using jQuery.getScript() works well.如果其他人遇到此问题 - 使用jQuery.getScript()效果很好。 To enable caching it's best to do as below:要启用缓存,最好执行以下操作:

$.ajax({
  url: url,
  dataType: "script",
  success: success,
  cache: true
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM