4

JavaScript errors to an alert box injure my soul. Is there a way to send it to console.log() instead of alert()?

4

4 回答 4

4

您可以覆盖 window.alert:

var oldAlert = window.alert; // reference to the original window.alert

window.alert = function(message) { 
  if (window.console && console.log) { 
    console.log(message); 
  } else { 
    oldAlert(message); // if console.log doesn't exist call window alert
  } 
} 
于 2009-08-18T22:44:19.157 回答
0

您可以尝试在 environment.rb 文件中关闭 RJS 调试:

config.action_view.debug_rjs = false

这应该完全关闭警报。另一种选择是覆盖ActionView::Helpers::GeneratorMethods#to_s

module ActionView
  module Helpers
    module GeneratorMethods
      def to_s #:nodoc:
        returning javascript = @lines * $/ do
          if ActionView::Base.debug_rjs
            source = javascript.dup
            javascript.replace "try {\n#{source}\n} catch (e) "
            javascript << "{ console.log('RJS error:\\n\\n' + e.toString()); console.log('#{source.gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }}'); throw e }"
          end
        end
      end
    end
  end
end

我承认,就覆盖该方法而言,我已经超出了我的深度,我不知道这是否是推荐的做法。我不需要在我的项目中做这样的事情。

我个人的偏好是跳过 RJS 并使用不显眼的 jQuery。

于 2009-08-19T01:43:18.950 回答
0

Yes, console.log("error message goes here")

于 2009-08-18T22:20:29.767 回答
0

It depends on the Javascript error. Since JavaScript is interpreted by the client, you would need to display the error messages client-side. The only real way to log errors in the server log file is if the error is happening on the server side before the Javascript is interpreted by the client.

于 2009-08-18T22:37:23.430 回答