【问题标题】:JavaScript running elsewhere but not in MediaWiki:common.jsJavaScript 在其他地方运行,但不在 MediaWiki:common.js 中
【发布时间】:2021-08-10 19:40:25
【问题描述】:

以下代码:

jQuery( function() {
  var kscript = document.createElement("script");
  kscript.src = 'https://assets-cdn.kf5.com/supportbox/main.js?' + (new Date()).getDay();
  kscript.setAttribute("id","kf5-provide-supportBox");
  kscript.setAttribute("kchat-id","1041075");
  kscript.setAttribute("kf5-domain","airbook.kf5.com");
  kscript.setAttribute("charset","utf-8");
  var s = document.getElementsByTagName('head')[0];
  s.appendChild(kscript);
});

当嵌入到纯 HTML 页面中时,它就像一个魅力。它呈现一个图标,点击后会触发一个 IM 对话框。

但是,当嵌入 MediaWiki:common.js 时,它不起作用。我可以清楚地看到生成并放入片段中的正确位置。然而,似乎什么也没发生。

Screenshot from DevTools

Test Page from a plain HTML page

Test Page from a Wiki page(中文但内容可以忽略)

【问题讨论】:

    标签: javascript html jquery mediawiki


    【解决方案1】:

    这是因为common.js中的JS加载时间较晚, 如果您在测试页面中的脚本周围添加 setTimeout,您会看到它也不会加载。在您的 wiki 中,您可以在 common.js 中添加它以使其工作:

    (function() {
      var kscript = document.createElement("script");
      kscript.src = 'https://assets-cdn.kf5.com/supportbox/main.js?' + (new Date()).getDay();
      kscript.setAttribute("id","kf5-provide-supportBox");
      kscript.setAttribute("kchat-id","1041075");
      kscript.setAttribute("kf5-domain","airbook.kf5.com");
      kscript.setAttribute("charset","utf-8");
      var s = document.getElementsByTagName('head')[0];
      s.appendChild(kscript);
      
      isKF5()
      
    })();
    
    function isKF5(){
    
      if (typeof KF5SupportBoxAPI !== 'undefined') {
          KF5SupportBoxAPI.init()
      } else {
        setTimeout( function(){
          isKF5()
        }, 100)
      }
      
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2013-11-06
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多