• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1703
  • Last Modified:

Using settimeout to give body focus

I have the following code which works great for me:

<body onblur="myfunction();" onload="this.focus();">

The problem is, it doesn't ALWAYS work great as the file with the body tag is within a frame, and sometimes things don't load in the order expected and myfunction() gets run instantly.

So I figured, okay, easy solution, just use setTimeout to make the body take the focus after,say, 5 seconds.

<script type="text/javascript">
// <![CDATA[
window.setTimeout("takefocus()", 5000);

function takefocus(){
      document.body.focus();
}
// ]]>
</script>

But it's not working.  The body is never getting focus, so my onblur function doesn't run.

What can I fix i that gets me the equivalent of
<body onload="this.focus();">
after 5 seconds?
Thanks!
0
dban00b
Asked:
dban00b
1 Solution
 
bluV11tCommented:
Try this:
<body onload="JavaScript: getFocusDelayed();">
 
//In <head> tag:
function getFocusDelayed(){
setTimeout("takefocus()", 5000);
}
 
function takefocus(){
this.focus();
}

Open in new window

0
 
scrathcyboyCommented:
put your javascript at the END of the document, just before the closing </BODY> tag.  The timeout will only work from there because it runs AFTER the page loads.  With it in the head, it runs before the page loads, so nothing happens.
0
 
dban00bAuthor Commented:
That code did exactly what I needed, thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now