posted 17 years ago
We have a request to set the focus into a portlet's textfield when the page loads. Since we don't have access to the HTML body tag to add 'on_load=document.myform.mytextfield.focus();', I tried doing the following in the endPage(). It doesn't work when the page is first displayed, but if you refresh the page by clicking on the selected tab, it does work. Can anyone explain why it only works sometimes?
PrintWriter writer = response.getWriter();
writer.println("<script for=\"window\" EVENT=\"on_load\" LANGUAGE=\"JavaScript1.2\">");
writer.println("try {");
writer.println("document.myform.mytextfield.focus();");
writer.println("}");
writer.println("catch(e) { }");
writer.println("</script>");
When I view the source of the HTML, it looks like this:
<script for="window" EVENT="on_load" LANGUAGE="JavaScript1.2">
try {
document.myform.mytextfield.focus();
}
catch(e) { }
</script>
FYI, We use WPS 5.1.