So, here's the picture: page1 has, at the bottom of it, a frame loading page2. The question is: can use a javascript (how?) in page2, so when the page1 loads, it will focus to the frame loading page2?
Thanks!

Recommended Answers

All 14 Replies

Frames don't have a focus. What exactly do you want to do? Also, do you really need a frame? Avoid them :)

I need them :)
All I want is above, maybe I can explain again using different words.. I want that when I load page1, it will focus on the page2 which is loaded by a frame at the bottom of page1. Also, I don't want to touch page1 in any way, I need this ONLY if the page2 can do this trick.
So again, page1 loads and focus on the frame containing page2, which (page2) contains the javascript (or else) to trigger the focus to happen - what is that script?
Thanks!

You can make an input field in the frame have focus:

document.getElementById('fieldid').focus()

You can make an input field in the frame have focus:

document.getElementById('fieldid').focus()

Can you be more explicit, please? Or better, provide the exact code I must use inside page2?
Thanks!

...a little later I got it. This does what I want:

<html>
<body onload="setFocus();">
<script language="JavaScript">
      function setFocus() {
            document.getElementById('link').focus();
           document.getElementById('link').value='xxx';
      }
</script>
<a id="link" href="http://xxx">xxx</a>
</body>
</html>

Thanks for the indications.

Nope, was wrong, it works partially ... only in IExplorer, not in Firefox, Chrome or Opera.

Change document to frame.contentWindow.document, where frame is the frame your element is in.

Doesn't work.
Changing that makes it not to work, not even in IE.

Sorry, didn't read carefully enough. You have to move your script to the head:

<html>
<head>
<script>
window.onload = function() {
  document.getElementById('link').focus();
};
</script>
</head>
<body>
<a id="link" href="http://xxx">xxx</a>
</body>
</html>

Edit: no, that's probably not what you mean. What was the goal of your .value change? Perhaps you mean innerHTML? Or href?

What was the goal of your .value change? Perhaps you mean innerHTML? Or href?

... sorry, I have no idea what are you talking about - my goal is what I wrote in the first post/thread :) ... just don't know how to accomplish this.

- Does it work if you do what I posted above?
- If not, what was the goal of document.getElementById('link').value='xxx'; ?

if document in frame2 is supposed to gain focus, why not <body onload='self.focus();'> in the document that is loading in frame 2

in frame1 <body onload='parent.frame2.focus();'>

Does that have any real meaning at all (for frames)? My guess was that an input needed focus.

Does that have any real meaning at all (for frames)? My guess was that an input needed focus.

you can apply focus to any element,
for this usage, focus is transferred to the body element within the frame, which of course fails(as expected) in any real sennse, leaving the frame in focus. <<--- what happens when you 'hmmm'' when trying to gather your thoughts and voice recognition is turned on
focus on an input item allows data entry, because the element is capable of entry
focus on an element that can do nothing, does nothing

Yes, that's what I mean.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.