Solved

javascript automatic execution

Posted on 2004-03-20
5
1,208 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
Comment Utility
<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
Comment Utility
setTimeout would be like a sleep copy paste and see results
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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…

772 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

9 Experts available now in Live!

Get 1:1 Help Now