后端

init.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * 动态加载初始资源
  3. */
  4. ;(function() {
  5. var resList = {
  6. icon: window.SITE_CONFIG.cdnUrl + '/static/img/favicon.ico',
  7. css: [
  8. window.SITE_CONFIG.cdnUrl + '/static/css/app.css',
  9. ],
  10. js: [
  11. window.SITE_CONFIG.cdnUrl + '/static/js/manifest.js',
  12. window.SITE_CONFIG.cdnUrl + '/static/js/vendor.js',
  13. window.SITE_CONFIG.cdnUrl + '/static/js/app.js'
  14. ]
  15. };
  16. // 图标
  17. (function () {
  18. var _icon = document.createElement('link');
  19. _icon.setAttribute('rel', 'shortcut icon');
  20. _icon.setAttribute('type', 'image/x-icon');
  21. _icon.setAttribute('href', resList.icon);
  22. document.getElementsByTagName('head')[0].appendChild(_icon);
  23. })();
  24. // 样式
  25. (function () {
  26. document.getElementsByTagName('html')[0].style.opacity = 0;
  27. var i = 0;
  28. var _style = null;
  29. var createStyles = function () {
  30. if (i >= resList.css.length) {
  31. document.getElementsByTagName('html')[0].style.opacity = 1;
  32. return;
  33. }
  34. _style = document.createElement('link');
  35. _style.href = resList.css[i];
  36. _style.setAttribute('rel', 'stylesheet');
  37. _style.onload = function () {
  38. i++;
  39. createStyles();
  40. }
  41. document.getElementsByTagName('head')[0].appendChild(_style);
  42. }
  43. createStyles();
  44. })();
  45. // 脚本
  46. document.onreadystatechange = function () {
  47. if (document.readyState === 'interactive') {
  48. var i = 0;
  49. var _script = null;
  50. var createScripts = function () {
  51. if (i >= resList.js.length) {
  52. return;
  53. }
  54. _script = document.createElement('script');
  55. _script.src = resList.js[i];
  56. _script.onload = function () {
  57. i++;
  58. createScripts();
  59. }
  60. document.getElementsByTagName('body')[0].appendChild(_script);
  61. }
  62. createScripts();
  63. }
  64. };
  65. })();