Solved

Using settimeout to give body focus

Posted on 2008-10-18
3
1,699 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

717 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