简体   繁体   English

Javascript错误隐藏在ASP.NET应用程序中

[英]Javascript errors are hidden in ASP.NET application

I have an ASP.NET(4.5) webform application which uses JavaScript . 我有一个使用JavaScriptASP.NET(4.5) Webform应用程序。 And I am using IE11 . 我正在使用IE11 The problem is if there is any error in this javascript(external file) nothing happens. 问题是,如果此javascript(外部文件)中没有任何错误,则什么也不会发生。

For example if I rename my file from a.js to b.js application continues running and nothing is showing in the IE. 例如,如果我将文件从a.js重命名为b.js,则应用程序将继续运行,而IE中则没有任何显示。

Or In my code I had: 或者在我的代码中,我有:

 <asp:LinkButton runat="server" OnClientClick="OpenWindow('.//Documents//TechnicalSupport.pdf');"  ..... >

And OpenWindow was not defined in the Javascript file so the link button was not working. 而且在Javascript文件中未定义OpenWindow,因此链接按钮不起作用。 From user perspective a click on the link did not do anything, no error messages or anything else. 从用户的角度来看,单击链接不会执行任何操作,也不会显示错误消息或其他任何内容。

The only way I can see it to press F12 and see what happened(OpenWindow is not defined). 我看到它的唯一方法是按F12键并查看发生了什么(未定义OpenWindow)。

Question : Is there any way to show the java script errors in the ASP.NET app? 问题:有什么方法可以显示ASP.NET应用程序中的Java脚本错误? In above case I want to at least be able to log error " OpenWindow is not defined " 在上述情况下,我至少希望能够记录错误“ OpenWindow is not defined

In response to your question 回答您的问题

Is there any way to show the java script errors in the ASP.NET app? 有什么方法可以显示ASP.NET应用程序中的Java脚本错误?

Yes, there is a way : 是的, 有一种方法

window.onerror = function(msg, url, linenumber) {
    //To show the error in the app
    alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
    //To show the error in the Javascript console (Press F12)
    console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
    return true;
}

You can't access a file directly in JS, but you could access by WebService or check this answer about PageMethods to Log the error in a file, just change the code in the static method 您不能直接在JS中访问文件,但可以通过WebService访问或检查有关PageMethods的答案以将错误记录在文件中,只需在静态方法中更改代码即可

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

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