By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,461 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,461 IT Pros & Developers. It's quick & easy.

Which is a good approach?

Dheeraj Joshi
Expert 100+
P: 1,123
Hi. I want to call 2 functions on body load.(JS functions).

So calling both on body load is good or call one function on body load and call other one at the end of first function?

i mean;

Expand|Select|Wrap|Line Numbers
  1. function my fun()
  2. {
  3.  
  4. }
  5.  
  6. function myfun2()
  7. {
  8.  
  9.  
  10. }
  11. <body onLoad="myfun();myfun2()">
  12.  
or

Expand|Select|Wrap|Line Numbers
  1. fuction myfun()
  2. {
  3.  
  4.  
  5. // Do all craps here.
  6.  
  7.  
  8. myfun2();
  9. }
  10.  
  11. function myfun2()
  12. {
  13.  
  14.  
  15. // Do all craps here.
  16.  
  17. }
  18.  
  19. <body onLoad="myfun()">
  20.  
Which approach is good.

Is there any advantage and disadvantage in both of these?

Regards
Dheeraj Joshi
Aug 15 '09 #1
Share this Question
Share on Google+
8 Replies


gits
Expert Mod 5K+
P: 5,390
that depends a bit on the 'craps' that should be done ... seen from a practical usage ... you could even use:

Expand|Select|Wrap|Line Numbers
  1. function initPage() {
  2.     myFun1();
  3.     myFun2();
  4. }
  5.  
and later on:

Expand|Select|Wrap|Line Numbers
  1. <body onload="initPage();">
which is a bit better since usally the scripts are in external files and it is good practice to have single entry and return points in your code ... but its even a bit of taste of the programmer ... i would use the method shown above ... since i think the methods typically should be self contained and in html there should only be one call ... so that i could have a look at the js-file and see what happens when initPage() is called ... i wouldn't destroy the 'semantics' of myFun1() with the call of the second function and might be you want to call it alone somewhere ... but when you call myFun2() within it then you need params or whatever to avoid the call of myFun2() sometimes ...

kind regards
Aug 15 '09 #2

Dheeraj Joshi
Expert 100+
P: 1,123
Thanks "GITS"

That was informative.

I think your approach to my problem is a good one.

Regards
Dheeraj Joshi
Aug 15 '09 #3

Dheeraj Joshi
Expert 100+
P: 1,123
Ok.. Back to actual problem.

I want to create few text boxes dynamically and also i want to incorporate auto update via AJAX.

So i need dynamic text box creation function and auto update function both to be called on body load.

Regards
Dheeraj Joshi
Aug 15 '09 #4

gits
Expert Mod 5K+
P: 5,390
ok ... and what is the problem?

kind regards
Aug 15 '09 #5

Dheeraj Joshi
Expert 100+
P: 1,123
Not back to actual problem.

But back to actual question.

The reason why i asked that question

Regards
Dheeraj Joshi
Aug 15 '09 #6

Dheeraj Joshi
Expert 100+
P: 1,123
I am starting to implement it.

If problem arises definitely post question.

Regards
Dheeraj Joshi
Aug 15 '09 #7

gits
Expert Mod 5K+
P: 5,390
ok :) ... i was just confused ;) ... i would suggest to start a new thread when you got problems with it ...

kind regards
Aug 15 '09 #8

Dheeraj Joshi
Expert 100+
P: 1,123
Yup.

I will start new thread. Hope you guys help me out, if i get problems while coding.

Regards
Dheeraj Joshi
Aug 15 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.