IE8/Firefox 行为差异
我正在开发以 JSP 形式编写的登录页面。 它非常简单,但在 IE8 和 Firefox 中的行为有所不同(这是一个很大的惊喜)。 我还没有测试过这是其他浏览器。
我可能可以用一些 Javascript 进行修复,但在我实施解决方法之前,我一直在寻找有关该行为的更多信息,希望能有一个更干净的修复并在将来避免这个问题。
有问题的服务器端生成的 HTML 片段是这样的:
<form name="member_session_info" method="post" autocomplete="off" action="/membersession" onsubmit="return validate_form()" >
<input type="hidden" id="memberSessionAction" name="memberSessionAction" value="" />
<input type="hidden" name="homePage" value="/2009/aas/aas_home.jsp" />
<input type="hidden" name="memberLandingPage" value="/2009/aas/aas_member_landing.jsp" />
<input type="hidden" name="memberProfilePage" value="/2009/aas/aas_member_profile.jsp" />
<input type="hidden" name="passwordRecoveryPage" value="/2009/aas/aas_password_recovery.jsp" />
<input id="uname" class="text xsmall right" name="username" value="USERNAME" onclick="checkClickUser()" onKeyPress="checkKeyPress(event, 'login', sendProfile)" style="width: 220px;" type="text">
<input id="pass" class="text xsmall right" name="password" value="PASSWORD" onclick="checkClickPassword()" onKeyPress="checkKeyPress(event, 'login', sendProfile)" style="width: 220px;" type="password">
<a href="javascript:sendProfile('startPasswordRecovery');" class="xsmall">FORGOT PASSWORD</a>
</form>
支持它的 Javascript 是:
<script type="text/javascript" language="JavaScript">
function validatePost(code, doAlert)
{
postValid = true;
return postValid;
}
function sendProfile(action)
{
document.getElementById("memberSessionAction").value = action;
document.member_session_info.submit();
return false;
}
function initializePage()
{
}
function validate_form()
{
return false;
}
function checkClickUser()
{
var username;
username = document.getElementById("uname").value;
if (username == "USERNAME") {
// Clear the field since it's the first time
document.getElementById("uname").value = "";
}
return true;
}
function checkClickPassword()
{
var username;
username = document.getElementById("pass").value;
if (username == "PASSWORD") {
// Clear the field since it's the first time
document.getElementById("pass").value = "";
}
return true;
}
function checkKeyPress(event, object, func)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (event) keycode = (event.which) ? event.which : event.keyCode;
else return true;
if ((keycode == 13)) // check for return
{
func(object);
return true;
}
return true;
}
</script>
基本症状是这样的: 如果您使用 Tab 从表单中的用户名字段导航到密码字段,则密码会在 FF 中正确突出显示并清除,但在 IE8 中则不然。 在 IE8 中,按 Tab 键移动到密码字段会将光标移动到密码框的最开头,保留默认值 (PASSWORD),而不清除它。
知道为什么会发生这种情况吗? 这是 IE8 的一个已知错误或固有缺陷吗?我只需要解决这个问题,或者我可以在某处添加一小段代码来更正确地处理 IE8 吗?
如果我的描述中问题不清楚,我可以尝试阐明或仅提供屏幕截图/视频剪辑,或在某处上传 HTML 的静态副本。 (如果是最后一个,我可以使用推荐的好网站或服务来执行此操作,因为实际网站仍处于开发阶段,尚未在网络上可用。)谢谢!
编辑:将 onclick 属性更改为 onfocus 解决了该问题,但又带来了另一个问题(请参阅我的评论@David)。 这可能与 checkKeyPress 的编写方式有关吗? 这是我从网站其他地方借用的功能。 我特别想知道更改其返回语句是否可以解决问题。 也许它不应该返回真/假/任何东西?
编辑2:我完全删除了checkKeyPress方法来查看这是否导致了问题,但它没有改变任何东西。
完整来源位于此处。 焦点随机跳转到的 div 是两个“全局导航”注释之间的那个,位于正文的最顶部。 仍然不知道为什么会发生。 为了查看焦点是否以某种方式重置,我在焦点随机跳转到的 div 上方添加了另一个 div,期望焦点开始跳转到新的 div。 事实并非如此。 它仍然将焦点切换到其中包含图像的 div。 我完全被迷惑了。
I'm working on login page written as a JSP. It's pretty simple but behaves differently in IE8 and Firefox (big surprise there). I have not tested this is other browsers yet.
I could probably hack a fix in with some Javascript but I was looking for more information about the behavior, before I just implement a workaround, with hopes of a cleaner fix and avoiding this problem in the future.
The server-side generated HTML snippet in question is this:
<form name="member_session_info" method="post" autocomplete="off" action="/membersession" onsubmit="return validate_form()" >
<input type="hidden" id="memberSessionAction" name="memberSessionAction" value="" />
<input type="hidden" name="homePage" value="/2009/aas/aas_home.jsp" />
<input type="hidden" name="memberLandingPage" value="/2009/aas/aas_member_landing.jsp" />
<input type="hidden" name="memberProfilePage" value="/2009/aas/aas_member_profile.jsp" />
<input type="hidden" name="passwordRecoveryPage" value="/2009/aas/aas_password_recovery.jsp" />
<input id="uname" class="text xsmall right" name="username" value="USERNAME" onclick="checkClickUser()" onKeyPress="checkKeyPress(event, 'login', sendProfile)" style="width: 220px;" type="text">
<input id="pass" class="text xsmall right" name="password" value="PASSWORD" onclick="checkClickPassword()" onKeyPress="checkKeyPress(event, 'login', sendProfile)" style="width: 220px;" type="password">
<a href="javascript:sendProfile('startPasswordRecovery');" class="xsmall">FORGOT PASSWORD</a>
</form>
and the Javascript that backs it up is:
<script type="text/javascript" language="JavaScript">
function validatePost(code, doAlert)
{
postValid = true;
return postValid;
}
function sendProfile(action)
{
document.getElementById("memberSessionAction").value = action;
document.member_session_info.submit();
return false;
}
function initializePage()
{
}
function validate_form()
{
return false;
}
function checkClickUser()
{
var username;
username = document.getElementById("uname").value;
if (username == "USERNAME") {
// Clear the field since it's the first time
document.getElementById("uname").value = "";
}
return true;
}
function checkClickPassword()
{
var username;
username = document.getElementById("pass").value;
if (username == "PASSWORD") {
// Clear the field since it's the first time
document.getElementById("pass").value = "";
}
return true;
}
function checkKeyPress(event, object, func)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (event) keycode = (event.which) ? event.which : event.keyCode;
else return true;
if ((keycode == 13)) // check for return
{
func(object);
return true;
}
return true;
}
</script>
The basic symptom is this:
If you use tab to navigate from the username field to the password field in the form, the password is correctly highlighted and cleared in FF, but not in IE8. In IE8, tabbing to the password field moves the cursor to the very beginning of the password box, leaving the default value (PASSWORD) in place, and not clearing it.
Any idea on why this occurs? Is this a known bug or inherent flaw of IE8 that I will just have to hack around, or can I just add a wee bit of code somewhere to handle IE8 more correctly?
If the problem isn't clear from my description I can attempt to elucidate or just throw up a screenshot/video clip, or upload a static copy of the HTML somewhere. (If the last one, I could use a recommendation of a good site or service to do this since the actual site is still in dev and not availabile to the web yet.) Thanks!
Edit: Changing the onclick property to onfocus fixed that problem, but brought another one to light (see my comment @David). Could this be related to the way that checkKeyPress is written? It's a function I borrowed from elsewhere in the site. In particular I'm wondering if changing its return statements could be the fix. Maybe it shouldn't return true/false/anything at all?
Edit 2: I removed the checkKeyPress method entirely to see if that was causing the problem, and it changed nothing.
The full source is here. The div that focus randomly jumps to is the one between the two "global nav" comments, at the very top of the body. Still no idea why it's happening. To see if the focus was somehow just getting reset, I added another div above the one that focus is jumping to randomly, expecting focus to start jumping to the new div instead. It didn't. It still switches focus to the div with the image in it. I am utterly befuggled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将检查放在 onfocus 而不是 onclick 中会怎样? 从技术上来说,切换到某个字段并不是一次点击。
What if you put the check in onfocus instead of onclick? Tabbing to a field technically isn't a click anyways.
由于失去焦点似乎每 6000 毫秒发生一次,因此我将责任归咎于 /js/qm_scripts.js 中的 Expandone()/contractall() 中的某个位置。
您的登录表单位于“dropmsg0”div 中,导致其短暂隐藏并每 6 秒重新显示一次。 文本框在 IE8 中隐藏时会失去焦点。 我要么重命名该 div 以从股票代码中排除 if,要么修改股票代码脚本以在只有一个 dropmsg div 时不运行。
Since the lost focus seems to happen every 6000 milliseconds, I'd point the blame somewhere at expandone()/contractall() in /js/qm_scripts.js.
Your login form is in the "dropmsg0" div, causing it to be briefly hidden and redisplayed every 6 seconds. The textboxes lose focus in IE8 when hidden. I'd either rename the div to exclude if from the ticker, or modify the ticker script to not run when there's only one dropmsg div.