admin管理员组文章数量:1356914
I'm capturing the window.onbeforeunload event using the following code:
window.onbeforeunload = function (evt)
{
if(checkIsDirty())
{
var message = 'If you continue your changes will not be saved.';
if (typeof evt == 'undefined')
{
//IE
evt = window.event;
}
if (evt)
{
evt.returnValue = message;
}
else
{
return message;
}
}
}
When I click 'Cancel' in the resulting confirmation, I get the error "Unknown Exception" and the following is highlighted by the debugger:
onclick="location.href='/<WhicheverPageYouRequested>.aspx';
If I click OK, the page changes perfectly. Why am I getting this error, and how can I fix it?
I'm capturing the window.onbeforeunload event using the following code:
window.onbeforeunload = function (evt)
{
if(checkIsDirty())
{
var message = 'If you continue your changes will not be saved.';
if (typeof evt == 'undefined')
{
//IE
evt = window.event;
}
if (evt)
{
evt.returnValue = message;
}
else
{
return message;
}
}
}
When I click 'Cancel' in the resulting confirmation, I get the error "Unknown Exception" and the following is highlighted by the debugger:
onclick="location.href='/<WhicheverPageYouRequested>.aspx';
If I click OK, the page changes perfectly. Why am I getting this error, and how can I fix it?
Share Improve this question edited Nov 5, 2009 at 15:38 Jason Berkan 8,9147 gold badges31 silver badges39 bronze badges asked Oct 2, 2009 at 13:54 djdd87djdd87 68.5k29 gold badges159 silver badges197 bronze badges1 Answer
Reset to default 9Yes, it's a known IE behaviour. When you try to navigate from JavaScript (using location, location.href, location.replace, document.location, history, even roundabout methods like link.click or form.submit), a beforeunload
Cancel will raise an exception. The test case is as simple as:
window.onbeforeunload= function() { return '?'; }
location.href= 'http://www.example./';
This might even be deliberate, to let the script know the navigation attempt failed, except that the error name is so pletely useless. (I get “Unspecified Error”.)
The traditional solution is just to catch it:
try {
location.href= '...';
} catch(e) {}
I'm always suspicious of that though as catching all exceptions is generally bad news (but IE doesn't allow you to only catch this exception... short of checking for the exact stringTo, which is horribly fragile.
Another approach is to move the onbeforeunload work to the calling code so that the real onbeforeunload never happens and there is no exception:
function navigate(url) {
if (window.onbeforeunload) {
if (confirm(window.onbeforeunload()+' Press OK to continue, or Cancel to stay on the current page.')
window.onbeforeunload= '';
else
return;
}
location.href= url;
}
However either solution may be beyond your grasp if, as seems likely, it is whatever framework you are using that is inserting the code, rather than it being something you've written yourself.
onclick="location.href='/<WhicheverPageYouRequested>.aspx'";
what sort of control is this? It looks suspicious in itself to be honest: surely something you click on to go to another page should be marked up as a simple link, and not a JavaScript-onclick element (these go wrong in many ways). If it were a simple non-JavaScript link you wouldn't have to worry about the IE error.
Incidentally, your beforeunload function will probably fail on non-IE browsers (returnValue is an IE-only thing). It also seems a bit over-plicated... what's wrong with:
window.onbeforeunload= function() {
if(checkIsDirty())
return 'If you continue your changes will not be saved.';
}
本文标签: aspnetquotUnknown Exceptionquot when cancelling page unload with quotlocationhrefquotStack Overflow
版权声明:本文标题:asp.net - "Unknown Exception" when cancelling page unload with "location.href" - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744063337a2584532.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论