解決 JS 部分語法在不同瀏覽器的相容性問題
  • 1,462 views,
  • 2016-12-27,
  • 上傳者: Kuann Hung,
  •  0
30abd3808b3d42cb3b0e7c63a5cd3074.png
由於在 Chrome, IE, Firefox, Opera 還有 Safari 都有一些 console 語法不盡相同,為了避免一執行到就掛點,建議可以在你的 js 中加上以下語法,這樣就不會遇到在程式中忘了移除 console.log,結果在 IE8 就直接死掉的問題了!!
 
(function() {
  // Union of Chrome, Firefox, IE, Opera, and Safari console methods
  var methods = ["assert", "cd", "clear", "count", "countReset",
    "debug", "dir", "dirxml", "error", "exception", "group", "groupCollapsed",
    "groupEnd", "info", "log", "markTimeline", "profile", "profileEnd",
    "select", "table", "time", "timeEnd", "timeStamp", "timeline",
    "timelineEnd", "trace", "warn"];
  var length = methods.length;
  var console = (window.console = window.console || {});
  var method;
  var noop = function() {};
  while (length--) {
    method = methods[length];
    // define undefined methods as noops to prevent errors
    if (!console[method])
      console[method] = noop;
  }
})();
Facebook 討論區載入中...
資料夾 :
發表時間 :
2016-12-27 23:30:24
觀看數 :
1,462
發表人 :
Kuann Hung
部門 :
老洪的 IT 學習系統
QR Code :