Solved

Using settimeout to give body focus

Posted on 2008-10-18
3
1,694 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
3 Comments
 
LVL 7

Accepted Solution

by:
bluV11t earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
That code did exactly what I needed, thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now