?
Solved

Using settimeout to give body focus

Posted on 2008-10-18
3
Medium Priority
?
1,700 Views
Last Modified: 2010-05-18
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
Comment
Question by:dban00b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Accepted Solution

by:
bluV11t earned 2000 total points
ID: 22749441
Try this:
<body onload="JavaScript: getFocusDelayed();">
 
//In <head> tag:
function getFocusDelayed(){
setTimeout("takefocus()", 5000);
}
 
function takefocus(){
this.focus();
}

Open in new window

0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22749706
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
 
LVL 1

Author Comment

by:dban00b
ID: 22750073
That code did exactly what I needed, thanks.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question