OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

In JavaScript setting a textarea with focus() does not work if called as a child window

  • Thread starter Thread starter Ian Smith
  • Start date Start date
I

Ian Smith

Guest
I have a simple test page that sets the focus to a textarea on an oninit function. However the exact code fails to do this if the page is called as a child.

Putting alert box proves that the oninit function is being called but fails to put the focus in the textbox. Pressing reload though does then focus correctly.

So given that my code works perfectly when called on a main page, and also works in a child if reload is called, then why doesn't it work the first time?

Code:
<html>
<body onload="init()">
<script type="text/javascript">
function init()
{
    document.getElementById("message").focus();
}

</script>
<textarea id="message" rows=10 cols=40></textarea>
</body>
</html>

Nothing clever here as you can, just only doesn't work if the page is loaded by window.open("test2.html");

<p>I have a simple test page that sets the focus to a textarea on an oninit function. However the exact code fails to do this if the page is called as a child.</p>

<p>Putting alert box proves that the oninit function is being called but fails to put the focus in the textbox. Pressing reload though does then focus correctly.</p>

<p>So given that my code works perfectly when called on a main page, and also works in a child if reload is called, then why doesn't it work the first time?</p>

<pre><code><html>
<body onload="init()">
<script type="text/javascript">
function init()
{
document.getElementById("message").focus();
}

</script>
<textarea id="message" rows=10 cols=40></textarea>
</body>
</html>
</code></pre>

<p>Nothing clever here as you can, just only doesn't work if the page is loaded by window.open("test2.html");</p>
 

Latest posts

Top