954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Focus and frames - hard question

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!

wattaman
Junior Poster in Training
95 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

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!

wattaman
Junior Poster in Training
95 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

document.getElementById('fieldid').focus()
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

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.

wattaman
Junior Poster in Training
95 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

wattaman
Junior Poster in Training
95 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

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

wattaman
Junior Poster in Training
95 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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?

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 
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.

wattaman
Junior Poster in Training
95 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

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();'>

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

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

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 
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, focusis 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

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

Yes, that's what I mean.

twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: