Solved

Using settimeout to give body focus

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lazy load not working with masonry js 1 22
jQuery on Submit 4 42
removing a class in javascript 4 49
javascript - insert into js doc help 2 25
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

828 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