Login | Iscriviti | FAQ
Anonymous

Calling an Initialization Function on Page-load

+ Rispondi al messaggio

2 messaggi Pagina 1 di 1


Calling an Initialization Function on Page-load

da mike_c » dom feb 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!
Mike.
Avatar utente

mike_c

  • Messaggi: 1
  • Iscritto il: mer feb 06, 2013 8:58 pm

Re: Calling an Initialization Function on Page-load

da XainPro » lun feb 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 utente

XainPro

  • Messaggi: 3933
  • Iscritto il: ven feb 17, 2012 8:10 pm


+ Rispondi al messaggio

Pagina 1 di 1