简体   繁体   中英

Javascript errors are hidden in ASP.NET application

I have an ASP.NET(4.5) webform application which uses JavaScript . And I am using IE11 . The problem is if there is any error in this javascript(external file) nothing happens.

For example if I rename my file from a.js to b.js application continues running and nothing is showing in the 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. 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).

Question : Is there any way to show the java script errors in the ASP.NET app? In above case I want to at least be able to log error " OpenWindow is not defined "

In response to your question

Is there any way to show the java script errors in the ASP.NET app?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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