Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Onload focus with id generated an error if id not found.Workaround?

Status
Not open for further replies.

celia05es

Programmer
Jan 10, 2002
81
0
0
US
Hello,
Well, I have a jsp file and the body line is:
Code:
<body onload="if (document.getElementById) document.getElementById('L1').focus();">
Now, if the file, I build a table. THere are several possible cases:
- Tuples can be deleted
- Tuples can be modified
- Tuples can be added
- Tuples can only be viewed.
In case tuples can me modified or deleted, I put the focus on the first tuples.

In case tuples can only be added, I put the focus on the tuples to be added.
Code:
<input type=text name=<%=name%> id="L1" tabindex=...>

My problem is that if a table can only be viewed, there is no "id"... I have tried to put the id="L1" on a hidden field but it does not work.
When IE loads the page, I get a "page error".... which is normal since there is no label "L1" defined....

Is there a workaround for that?

THanks for your help
 
There are many ways around it. Here are 2:

Code:
onload="try { document.getElementById('L1').focus(); }; catch(e) {};"

Code:
onload="if (document.getElementById) if (document.getElementById('L1')) document.getElementById('L1').focus();"

And I've got to ask... what is a tuple?

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I thought a Tuple was a baby Oompa-Loompa. Or was it a BUNCH of baby Oomp-Loompa's?

Stamp out, eliminate and abolish redundancy!
 
THank you so much !!

Well, I was thinking database when I wrote this.... so, in a relational database, a tuple is a record (a row)
 
OK, the same type of question.... after the <table> definition, I have:
Code:
<script language="javaScript">document.getElementById('L1').focus();</script>
How can I write it so it does not generate an error if the L1 does not exist!!!

I am not that good in Javascript as you have noticed!!!

THanks
 
Well, thinking a little bit, I suppose the lines you have written work anywhere !!! Right?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top