Solved

javascript automatic execution

Posted on 2004-03-20
5
1,209 Views
Last Modified: 2012-08-13
I have
<script>
 //javascript functions
</script>

1. Is there a way to run one of the functions automatically whenever the page is loaded, besides BODY onLoad event ??

2. Is there in javascript Sleep function that  makes thread to sleep for given time?
0
Comment
Question by:amakalski
5 Comments
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10641970
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
window.onload = function(){
      // Do your onload statements here
      alert("Some onload function is running");
      setTimeout("alert('I was sleeping for just a few :)')", 700);
}
// -->
</script>
</head>

<body>

</body>
</html>
0
 
LVL 16

Assisted Solution

by:jaysolomon
jaysolomon earned 75 total points
ID: 10641974
setTimeout would be like a sleep copy paste and see results
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 75 total points
ID: 10642045
For your first question.... you can directly do the function call without onload() function..

<script>

welcomeuser();

 function welcomeuser()
 {
    alert("welcome...");
 }

</script>
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10642384
Adding on to what venkateshwarr said...

If you want to wait until the page COMPLETELY loaded, make sure you put venkateshwarr's code at the very bottom of your page. If you don't, you might get an error. For example, the following code throws an error because the script runs before the form has loaded.

<script>
welcomeuser();
function welcomeuser(){
document.forms[0].welcome.value="Welcome!";
}
</script>

<form>
<input type=text name=welcome>
</form>
0
 
LVL 15

Expert Comment

by:JakobA
ID: 10642741
Also you should not call the function before you define it:

<script>
function welcomeuser(){    // defines the function
    document.forms[0].welcome.value="Welcome!";
}
          // now the function has been defined so we can call it:
welcomeuser();
</script>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now