0

My function is a timer that should run when the page is displayed. I wrote

<script>
    startcountdown();
</script>

In html but this doesn't work. How can I call that function? I don't need special event to happen before calling it.

10
  • Where the function is defined? Commented May 3, 2016 at 11:05
  • If startcountdown is manipulating DOM, you must wait for the DOM to be ready! Commented May 3, 2016 at 11:06
  • 1
    write your code at the end. Commented May 3, 2016 at 11:06
  • Its defined in a script file,... <script type="text/javascript" src="scripts.js"></script> Commented May 3, 2016 at 11:07
  • 1
    Check console for errors ? Also define doesn't work Commented May 3, 2016 at 11:14

5 Answers 5

5
<script>
window.onload = function() {
   startcountdown();
}
</script> 
Sign up to request clarification or add additional context in comments.

Comments

4

You may use JavaScripts native window.onload function, if you are not using any other library like jQuery or something else.

<script>
  window.onload = function() {
    startcountdown();
  };
</script>

I'd also like to mention that you could also start a function by just adding it to the end of your markup. That being said, if you define a function function startcountdown() { } at the very beginning of your html, you could simply use startcountdown(); at the very end of your html (e.g. before your closing </body> tag).

This approach would simply execute your function after your DOM has being loaded, since it's defined as the last call of your markup.

Comments

2

you can put it into tag using 'onload' event:

or just use jQuery:

$(document).ready(function() {
    yourFunct();
});

Comments

1

you can try this put onload="startcountdown();" in html body tag

Comments

0

By calling your function in page on-load

<script>
window.onload = startcountdown;
 function startcountdown(){
   //write your code here
}
</script> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.