1

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"

S Nash
  • 2,363
  • 3
  • 34
  • 64
  • Yes, you need to open the browser's debug console (F12). – Jasen Jun 12 '15 at 17:11
  • have you tried the window.onerror event? window.onerror = function myErrorHandler(errorMsg, url, lineNumber) { alert("Error occured: " + errorMsg);//or any message return false; } – Sushil Jun 12 '15 at 17:14

1 Answers1

2

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

Community
  • 1
  • 1
Enrique Zavaleta
  • 2,098
  • 3
  • 21
  • 29
  • Yes I can access a file direcltly in the JS.:: `OpenWindow('.//Documents//TechnicalSupport.pdf');` Works without a problem – S Nash Jun 12 '15 at 18:10
  • window.onerror only provides error for window object. how about a case when calling a javascript function causes an error? – S Nash Jun 12 '15 at 18:12
  • No, OpenWindow just open a file (PDF, in another cases it downloads the file) with the browser, but no write / read the file in JS. And to catch another code, use TryCatch blocks in your code (when calling a JS function) – Enrique Zavaleta Jun 12 '15 at 19:50