473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getting focus to document element on load

I'm trying to get an <a> element to gain the focus onload, but only get
back 'has no properties'.
Reading through this ng, its clear that unless the element is within a
form, this doesn't happen, but according to the w3 TR on html 4.01, the a
element can take onFocus commands, which to me means you can do
document.getElementById(element).focus() ?

I'm trying to get the focus to a specific paragraph, so the page scrolls to
it on loading, but as the page in question is dynamic, i can't feed the #
van the URL
Jul 23 '05 #1
9 9478
s_m_b wrote:
I'm trying to get an <a> element to gain the focus onload, but only get
back 'has no properties'.
Reading through this ng, its clear that unless the element is within a
form, this doesn't happen, but according to the w3 TR on html 4.01, the a
element can take onFocus commands, which to me means you can do
document.getElementById(element).focus() ?
<a> elements have onfocus intrinsic events, which means you can make
things happen when they gain focus, usually by tabbing to or clicking
on them. It does not mean the element has a focus() method.

I'm trying to get the focus to a specific paragraph, so the page scrolls to
it on loading, but as the page in question is dynamic, i can't feed the #
van the URL


That is what anchors are for. Giving the <a> focus will not
necessarily scroll the page to it, but using it as an anchor will if
there is sufficient depth in the page.

<URL:http://www.w3.org/TR/html401/struct/links.html#h-12.1>

--
Rob
Jul 23 '05 #2
RobG <rg***@iinet.net.auau> wrote in
news:42***********************@per-qv1-newsreader-01.iinet.net.au:
s_m_b wrote:
I'm trying to get an <a> element to gain the focus onload, but only
get back 'has no properties'.
Reading through this ng, its clear that unless the element is within
a form, this doesn't happen, but according to the w3 TR on html 4.01,
the a element can take onFocus commands, which to me means you can do
document.getElementById(element).focus() ?
<a> elements have onfocus intrinsic events, which means you can make
things happen when they gain focus, usually by tabbing to or
clicking on them. It does not mean the element has a focus()
method.


ah - that explains it.

I'm trying to get the focus to a specific paragraph, so the page
scrolls to it on loading, but as the page in question is dynamic, i
can't feed the # van the URL


That is what anchors are for. Giving the <a> focus will not
necessarily scroll the page to it, but using it as an anchor will if
there is sufficient depth in the page.

<URL:http://www.w3.org/TR/html401/struct/links.html#h-12.1>


yes, that I know, but if you add '?x=y' into the URL it doesn't work any
more. You either get the contents of the GET ignored or the anchor
reference is.
Jul 23 '05 #3
I'm having a similar situation with normal text links. For example,
after closing an iframe or hiding a division I want focus to go to a
certain text link. I tried:

document.links[2].focus;

to target the 3rd link on the page after the closing or hide, and it
doesn't work. Is this possible ?

Later, Art.

Jul 23 '05 #4
In my previous post I forgot to point out that the:

document.links[2].focus;

is not part of a function. It's a stand alone statement within the
script tags. Might that be the problem ?

Jul 23 '05 #5
s_m_b wrote:
RobG <rg***@iinet.net.auau> wrote in
news:42***********************@per-qv1-newsreader-01.iinet.net.au:

s_m_b wrote:
I'm trying to get an <a> element to gain the focus onload, but only
get back 'has no properties'.
Reading through this ng, its clear that unless the element is within
a form, this doesn't happen, but according to the w3 TR on html 4.01,
the a element can take onFocus commands, which to me means you can do
document.getElementById(element).focus() ?


<a> elements have onfocus intrinsic events, which means you can make
things happen when they gain focus, usually by tabbing to or
clicking on them. It does not mean the element has a focus()
method.

ah - that explains it.

I'm trying to get the focus to a specific paragraph, so the page
scrolls to it on loading, but as the page in question is dynamic, i
can't feed the # van the URL


That is what anchors are for. Giving the <a> focus will not
necessarily scroll the page to it, but using it as an anchor will if
there is sufficient depth in the page.

<URL:http://www.w3.org/TR/html401/struct/links.html#h-12.1>


yes, that I know, but if you add '?x=y' into the URL it doesn't work any
more. You either get the contents of the GET ignored or the anchor
reference is.


I think you need to explain a bit more about what you are trying to
do - do you have bit of code showing what you are up to?

--
Rob
Jul 23 '05 #6
Art X wrote:
I'm having a similar situation with normal text links. For example,
after closing an iframe or hiding a division I want focus to go to a
certain text link. I tried:

document.links[2].focus;

to target the 3rd link on the page after the closing or hide, and it
doesn't work. Is this possible ?

Later, Art.


Give the a element a name, that makes it an anchor. Fire some event
and set the page location to <currentURL> + '#' + <anchorName>.

That will navigate to the link and put it in focus:

<a href="#freddy">freddy</a>

<br><br><br><br><br><br><br><br><br><br><br><br><b r><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><b r><br><br>

<a name="freddy" href="http://www.apple.com" onfocus="
alert('Hey, ' + this.name + ' got focus');">Apple</a>

--
Rob
Jul 23 '05 #7
Art X wrote:
In my previous post I forgot to point out that the:

document.links[2].focus;

is not part of a function. It's a stand alone statement within the
script tags. Might that be the problem ?


I ignored it. If you were trying to call the focus method, then:

document.links[2].focus();

will do the trick, but using an anchor will work without JavaScript.
Probably should test the method before trying to use it:

if (document.links[2].focus) document.links[2].focus();

--
Rob
Jul 23 '05 #8
Thanks Rob,

I'll try the anchor.

Later, Art.

Jul 23 '05 #9
RobG <rg***@iinet.net.auau> wrote in
news:42***********************@per-qv1-newsreader-01.iinet.net.au:
s_m_b wrote:
RobG <rg***@iinet.net.auau> wrote in
news:42***********************@per-qv1-newsreader-01.iinet.net.au:

s_m_b wrote:

I'm trying to get an <a> element to gain the focus onload, but only
get back 'has no properties'.
Reading through this ng, its clear that unless the element is within
a form, this doesn't happen, but according to the w3 TR on html
4.01, the a element can take onFocus commands, which to me means you
can do document.getElementById(element).focus() ?

<a> elements have onfocus intrinsic events, which means you can
make things happen when they gain focus, usually by tabbing to or
clicking on them. It does not mean the element has a focus()
method.

ah - that explains it.

I'm trying to get the focus to a specific paragraph, so the page
scrolls to it on loading, but as the page in question is dynamic, i
can't feed the # van the URL

That is what anchors are for. Giving the <a> focus will not
necessarily scroll the page to it, but using it as an anchor will
if there is sufficient depth in the page.

<URL:http://www.w3.org/TR/html401/struct/links.html#h-12.1>


yes, that I know, but if you add '?x=y' into the URL it doesn't work
any more. You either get the contents of the GET ignored or the
anchor reference is.


I think you need to explain a bit more about what you are trying to
do - do you have bit of code showing what you are up to?


ah..............

I've just been through the code in a little more depth to check it, and
found the problem - the anchor I was sending is missing on the target
document. Another anchor that I've just tried, does exist and works.
I'll have to find where this duff one comes from now [blush]
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mica Cooper | last post by:
Trying to set the focus to an element of input type on loading the page. This is the code I used but it doesn't work. Any suggestions? (IE 6, Windows) <SCRIPT language=JavaScript> funcion...
6
by: Csaba | last post by:
I'd like to have the cursor in the login form's username input text field when users load the login page. It works fine with the following implementation: <body...
18
by: lawrence | last post by:
If I'm pretty sure there is just one form on the page, can i do this? var myForm = document.forms; If I'm not sure about the form, is it safer to do this? if (document.forms) { var myForm =...
2
by: Peter Wright | last post by:
Hi all. Hopefully this should demonstrate the problem I'm having: http://flooble.net/~pete/focus-problem-demo/ (I'm testing it in Mozilla only, but I'm not sure if it's actually a...
3
by: Praveen | last post by:
In IE a table element will receive focus when you either tab into it or when you click anywhere within the table. Mainly it fires the onfocus event. This doesn't happen in Mozilla (Firefox and...
0
by: Vinod. | last post by:
Hi all, I have added browser control to a windows form. I am loading pages having multiple frames. I have noticed that the focus in a text is not maintained when the application is deactivated....
1
by: bagya | last post by:
please help me out the following is the small code i have <html> <head> <script type="text/javascript"> function validate() { if (document.abc.region.value==0)
2
dlite922
by: dlite922 | last post by:
Before traversing my code, here's what my goal is and what this function does: I have a table of fields that dynamically grows as the user enters information. A minimum of 3 rows must always...
4
Claus Mygind
by: Claus Mygind | last post by:
I am creating an element dynamically as I load it on the screen. When it was hard coded on the page I had no problem executing the following two lines. document.getElementById("SEARCH").select();...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.