Jump to content

layout of javascript in website

- - - - -

  • Please log in to reply
5 replies to this topic

#1
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
So, its been a long time since I've been on this site, but I've been lurking again recently and now have a question....

I've never really made a website before, but now I've been tasked with making a webpage that does various things with javascript.

I was wondering what the best approach to laying it all out would be? I've gathered that its a good idea to wrap up the code in functions, and put them in the head, but then some javascript was dependent on the html rendering first, I think document.getElement is..?
at the moment the majority of the code is at the bottom of the page, but its getting messy.

I'm aware this question is slightly open ended, I'm just looking for some good practices and habits. I can code quite well in other languages, not used to this mixing of them on the same page though

#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
What I usually do (mind you it may not be the best way to do things) is keep all functions and stuff on a separate scripts.js which I then link to the html document on the header.

I never put "loose" scripts on the page. I always tie a javascript action to an event such as body.onload for actions to perform as soon as the page loads or element.onclick for when you click on a certain element.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If you wrap all the logic in functions, then you can trigger calling them based on the document rendering being complete. The onload event in <body> is usually a good option.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Thanks for the replies. I have been considering seperate js files, will probably have to start using them when I gather enough code. Ah thanks for that wingedPanther, I shall use that. I have now also included some vbscript, mostly because I have to, which is a total pain in the arse

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Fair warning: vbscript only runs in IE, not any other browser. You would do well to avoid it for client-side scripting.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
yeah unfortunately thats why I have to use it, I'm trying to test for active X plugins, not easy when there doesn't seem to be a list of the object names anywhere. But thats what I've come to expect from anything IE related. In FF I can just access the navigator array, I hope its that simple in chrome and safari. Its also the first time I've really used a basic language, I didn't realize just how different they are, things like & being used to concatenate strings instead of +.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users