使用onLoad()来集中登录表单 - 错误处理 [英] using onLoad() to focus login form- error handling

查看:53
本文介绍了使用onLoad()来集中登录表单 - 错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修复错误 - 对象不存在 - 我希望有可能

允许对象不存在。


我正在写一个页面上的脚本可能包含也可能不包含登录

表格。例如,在访问者登录后,页面上的登录表单不再是
,但其他内容仍然存在。

我想将重点放在登录页面上使用身上的onLoad形式

标签.....但如果登录表格不存在,我不想执行

这个功能。


以下代码适用于将光标聚焦到登录

表格。


(代码)

< html>

< head>

< title>

测试Javascript

< / title>

< script type =" text / javascript">

<! -

function addcursor ()

{

if(document.login){

document.login.username.focus();

}

}

// - >

< / script>

< / head>

< body OnLoad =" addcursor()">


< form name =" login" ID = QUOT;登录"行动= QUOT;" method =" post">

< input type =" text"命名= QUOT;用户名" ID = QUOT;用户名" />

< input type =" password"名称= QUOT;密码" />


< / form>

< / body>

< / html>

(结束代码)


但是如果从代码中移除body部分中的表单,则会出现错误

对象不存在或者为空结果。

我正在寻找避免此错误的方法。

有什么建议吗?

I am trying to fix error- object does not exist- I want it possible to
allow object not to exist.

I am writing a script on a page that may or may not include a login
form. For example-after a visitor logs in, the login form is no longer
on the page, but other content is still there.
I want to bring focus to the login form using the onLoad in the body
tag.....but if the login form does not exist, I do not want to perform
this function.

The following code works fine for focusing the cursor on to the login
form.

(code)
<html>
<head>
<title>
Testing Javascript
</title>
<script type="text/javascript">
<!--
function addcursor ()
{
if (document.login) {
document.login.username.focus();
}
}
//-->
</script>
</head>
<body OnLoad ="addcursor()">

<form name="login" id="login" action="" method="post">
<input type = "text" name="username" id="username" />
<input type="password" name="password" />

</form>
</body>
</html>
(end code)

But if you remove the form in the body section from the code, an error
"object does not exist or is null" results.
I am looking for a way to avoid this error.
Any suggestions?

推荐答案

问候,


您可能会想要将函数调用移出body标签并且

将它放在底部包含表单的页面。你可以按照以下方式进行onload调用:


< script type =" text / javascript">

window.onload = function(){

document.login.username.focus();

}

< / script>


将它放在包含表格的页面底部,它将与您现有的相同。

。另外,这会让你感到害怕

朝着禁用javascript警告的脚本前进。


我希望这会有所帮助,


- Peter Schmalfeldt

Manifest Interactive

Greetings,

You might juse want to move the function call out of the body tag and
place it at the bottom of the page that has the form on it. You can
still do an onload call as follows:

<script type="text/javascript">
window.onload = function() {
document.login.username.focus();
}
</script>

Place this at the bottom of your page that has the form and it will do
the same as what you currently have. Plus this will keep you frma
heading towards a script that disables javascript alerts.

I hope this helped,

- Peter Schmalfeldt
Manifest Interactive


Manifest Interactive写道:
Manifest Interactive wrote:
您可能希望将函数调用移出body标签,并将其放在页面底部的表单上。您仍然可以按如下方式进行onload调用:

< script type =" text / javascript">
window.onload = function(){
document.login.username.focus();
}
< / script>

将它放在页面底部,其中包含[...]表格
You might juse want to move the function call out of the body tag and
place it at the bottom of the page that has the form on it. You can
still do an onload call as follows:

<script type="text/javascript">
window.onload = function() {
document.login.username.focus();
}
</script>

Place this at the bottom of your page that has the form [...]




不,不,不。使用


< body onload =" document.forms [''login'']。elements [''username'']。focus();">

...

< / body>


代替。

PointedEars



No, don''t. Use

<body onload="document.forms[''login''].elements[''username''].focus();">
...
</body>

instead.
PointedEars


这将导致该用户遇到的同样问题。由于

代码将放置在每个页面上,因此某些页面将不具有名为login的形式

显然也没有元素

" username"。加载页面时脚本会生成错误,

这是原帖要求修复的内容。

This will cause the same problem that this user was having. Since the
code would be placed on every page, some pages will not have the form
named "login" which obviously would also not have the element
"username". The script will generate an error when the page is loaded,
which is what the original post requested to fix.


这篇关于使用onLoad()来集中登录表单 - 错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆