Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.js file format

I was wondering if there is a good reference to how best to format .js files?

Do you treat them like you would class files or do you separate out the functionality based off the page it handles? Do you create .js functions that could be used across several pages or do you write a custom .js file for each page?

I'm just curious if there is a formal way rather than just writing a bunch of functions that may or may not be group together for a specific page or set of pages. Right now I try to group by the functionality of the page but my .js files are very custom for that page, not sure if they are usable on a different page.

::UPDATE::

Some of the answers have the same theme and mostly about what you plan on doing with the project. Right now the project is real new to me and the requirements are slow in coming. So I'm building it page by page. There could be cross functionality or similar functionality in the future. Most of the functionality is very specific to the specific page though.

Is it wise to group AJAX calls into a .js but then also have a custompage .js that contains all of the very custom features of the page.

like image 371
webdad3 Avatar asked Apr 06 '26 05:04

webdad3


2 Answers

Typically with javascript you'll want to reduce the amount of separate javascript files you include on your pages to reduce HTTP requests.

like image 62
Okeydoke Avatar answered Apr 08 '26 22:04

Okeydoke


A great starting point would be for you to watch Rebecca Murphys building apps with jQuery presentation and read her blog post about using objects to organize your js.

like image 41
redsquare Avatar answered Apr 08 '26 21:04

redsquare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!