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"

2
  • Yes, you need to open the browser's debug console (F12). Commented Jun 12, 2015 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; } Commented Jun 12, 2015 at 17:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I can access a file direcltly in the JS.:: OpenWindow('.//Documents//TechnicalSupport.pdf'); Works without a problem
window.onerror only provides error for window object. how about a case when calling a javascript function causes an error?
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.