views:

2993

answers:

8

First, the background: I'm working in Tapestry 4, so the HTML for any given page is stitched together from various bits and pieces of HTML scattered throughout the application. For the component I'm working on I don't have the <body> tag so I can't give it an onload attribute.

The component has an input element that needs focus when the page loads. Does anyone know a way to set the focus to a file input (or any other text-type input) on page load without access to the body tag?

I've tried inserting script into the body like
document.body.setAttribute('onload', 'setFocus()')
(where setFocus is a function setting the focus to the file input element), but that didn't work. I can't say I was surprised by that though.

EDIT:
As has been stated, I do indeed need to do this with a page component. I ended up adding file-type inputs to the script we use for giving focus to the first editable and visible input on a page. In researching this problem I haven't found any security issues with doing this.

A: 

Try play with tabstop attribute

kingoleg
+1  A: 

This has worked well for me:

<script>
  function getLastFormElem(){
    var fID = document.forms.length -1;
    var f = document.forms[fID];
    var eID = f.elements.length -1;
    return f.elements[eID];
  }
</script>


<input name="whatever" id="maybesetmaybenot" type="text"/>
<!-- any other code except more form tags -->

<script>getLastFormElem().focus();</script>
scunliffe
A: 

you can give the window an onload handler

window.onload = setFocus;
ob
+1  A: 

I think you have a fundamental problem with your encapsulation. Although in most cases you could attach an event handler to the onload event - see http://ejohn.org/projects/flexible-javascript-events/ by John Resig for how to do this, setFocus needs to be managed by a page component since you can't have two components on your page requiring that they get the focus when the page loads.

Alohci
A: 

First of all, the input file is no the same as the other inputs, you need to keep this in mind.... thats for security reasons. When the input file get focus it should be read only or the browser should popup a dialog to choose some file.

Now, for the other inputs you could try some onload event on some of your elements...(not only the body have the onload event) or you could use inline javascript in the middle of the html. If you put javascript code without telling that is a function it gets executes while the browser reads it. Something like:

<script type="text/javascript">

function yourFunction()
{
   ...;
};

alert('hello world!");

yourFunction();

</script>

The function will be executed after the alert just when the browser reads it.

If you can, you should use jQuery to do your javascript. It will make your live soooo much easy.... :)

Lucas
Why should the file input be read only? What is the risk in allowing a user to type in the path?
ntownsend
It actually depends on the environment that you are working. Its a browser restriction. For example, mozilla people say this:http://www.mozilla.org/security/announce/2007/mfsa2007-32.html
Lucas
There are a lot of restrictions involving the input file...
Lucas
A: 

With jQuery could be done like this:

$(function() {
    $("input:file").eq(0).focus()
})

With plain javascript could be done like this:

var oldWindowOnload = window.onload; // be nice with other uses of onload 
window.onload = function() {
    var form = document.forms[0];
    for(i=0; i < form.length; i++) {
        if (form[i].type == "file") {
            form[i].focus();
        }
    }
    oldWindowOnload();
}

For more elaborate solution with plain javascript see Set Focus to First Input on Web Page on CodeProject.

Aleris
A: 

Scunliffe's solution has a usability advantage.

When page scripts are loading slowly, calling focus() from "onLoad" event makes a very nasty page "jump" if user scrolls away the page. So this is a more user friendly approach:

<input id="..."></input>
... really small piece of HTML ...
<script>getTheDesiredInput().focus();</script>
Sergey
A: 

kingoleg, I think you meant tabindex attribute(?) http://www.htmlcodetutorial.com/forms/%5FINPUT%5FTABINDEX.html

matt wilkie
Hi. It is much better to be added as a comment to the kingoleg answer. That's how stackoverflow works.
Sergey
yes, but you have to have 50 karma to be allowed to comment to an answer that is not your own. That too is how stack overflow works :P
matt wilkie
I see. Nice catch, Matt :)
Sergey