Zaloguj | Zarejestruj | FAQ

Calling an Initialization Function on Page-load

+ Odpowiedz

Posty: 2 Strona 1 z 1

Calling an Initialization Function on Page-load

przez mike_c » N lut 17, 2013 12:05 pm

Hey. I'm very new to JavaScript (just been learning it for a few weeks) and I'm currently working through the tutorials on this site - which are great; well written and make things really easy to understand.
As I'm learning the ins and outs of the language, there are a few questions that are coming up which I thought might be good to ask you guys on this board, one of which is:

When you create an initialization function and then set it to run when the page loads, all the tutorials so far have been putting this onload trigger after the init() function itself like so:

function init()
var myForm = document.getElementById("myForm");
myForm.onsubmit = validate;

onload = init;

I've tried running the script with the onload trigger above the function definition and it seems to work fine, no difference at all. So does it matter what order you put them in?
I ask this because since the page's onload event is the first thing that happens, I was thinking maybe the onload trigger should go first? Also, it seems that in most other cases, when a function is called, usually the function block of code goes after the call (at least when its called the first time).

This is a pretty basic question, but I was just wondering if it has implications for calling functions based on other page events. I did a search on the board and it doesn't look like this question has already been asked, so I wondered if you might be able to help.

Thanks for reading!
Avatar użytkownika


  • Posty: 1
  • Dołączył(a): Śr lut 06, 2013 8:58 pm

Re: Calling an Initialization Function on Page-load

przez XainPro » Pn lut 18, 2013 4:18 pm

Well it dose not matter because it will trigger the event after loading all the code of body tag
so tactically it is function is loading after it is being triggered
But i Think its good to write function before calling it.
Avatar użytkownika


  • Posty: 3933
  • Dołączył(a): Pt lut 17, 2012 8:10 pm

+ Odpowiedz

Strona 1 z 1