javascript 获取“用户提示中止”javascript异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8211225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:40:05  来源:igfitidea点击:

Getting "prompt aborted by user" javascript exception

javascript

提问by Bhagwat Gurle

I am getting "Components.Exception("prompt aborted by user", Cr.NS_ERROR_NOT_AVAILABLE)"exception when I am using "windows.location.href" in javasacript. My Code is:

当我在 javasacript 中使用“windows.location.href”时,我收到“Components.Exception(“prompt aborted by user”, Cr.NS_ERROR_NOT_AVAILABLE)”异常。我的代码是:

function checkCookie(){
var value = null;   
var cookieName='UserDetailsCookie'; 
value=ReadCookie(cookieName);   
if(value != null){          
    var url='<%=request.getContextPath()%>/jsp/admin.jsp';
    window.location.href = url;     
}
document.loginForm.userName.focus();

}

}

function ReadCookie(name)
{
  name += '=';
  var parts = document.cookie.split(/;\s*/);
  for (var i = 0; i < parts.length; i++)
  {
    var part = parts[i];
    if (part.indexOf(name) == 0)
          return part.substring(name.length);
      }
  return null;

}

}

and I am calling this method on onLoad event of body

我在 body 的 onLoad 事件上调用这个方法

<body onLoad="javascript:checkCookie();">

In anyone knows why this exception throws please?

有谁知道为什么会抛出这个异常?

回答by Huafu

I'm not 100% sure, but I think this is due to the line

我不是 100% 确定,但我认为这是由于线路

document.loginForm.userName.focus();

because this will cancel the loading of your URL done with the window.location.href = 'xxx'.

因为这将取消使用 window.location.href = 'xxx' 完成的 URL 加载。

Anyway, in general there should NOT be any line of code after setting the location.href property.

无论如何,通常在设置 location.href 属性后不应该有任何代码行。

Hope that helps, I know it's old but I answered for moomoochoo since his comment isn't "that" old.

希望有帮助,我知道它很旧,但我回答了 moomoochoo,因为他的评论不是“那么”旧。