ASP.NET catch referenced script file exceptions
How to catch 开发者_开发问答exceptions raised by a referenced javascript file in an aspx page??
You can't catch an exception that occurs on the user's browser from the web server. This is not due to some ASP.NET limitation, it's just that the code (javascript) that executes on the client has nothing to do with the code that executes on the server, probably thousands of miles away. If you want to catch and handle a javascript exception you should use javascript's try...catch mechanism
What are you trying to do? Are you trying to trace bugs in your code? Do you want to be notified if a client's browser can't handle your javascript? Or do you want to notify the server that something went wrong in the client?
EDIT What you are really looking for is a logging framework for javascript. There are multiple logging frameworks out there, some of which support logging to a remote server through Ajax, e.g. log4javascript and log4js .
Maybe you want to this.
<head runat="server">
<title></title>
<script type="text/javascript" src="JScript.js"></script>
</head>
<body>
<form id="form1" runat="server">
<button type="button" onclick="try{ testException();} catch(e){alert(e);}">Test Exception</button>
</form>
</body>
JScript.js
function testException() {
e.data = 10;
}
精彩评论