Hi all, I was wondering if anybody can enlighten me on when it is best to separate javascript code in different script. Let me give you a practical example.
I have a script that I would like to run everytime the page is resized, so I am thinking to use the onresize
event. But I also have some code that I will need to run when the DOM is ready, so I will wrap it inside a $(document).ready(function(){...});
I believe the script that is run onresize doesn't need to be inside a document ready, so is it worth splitting the 2 scripts into 2 separate files?
From here, in general, is it fair to say that the portion of a script that doesn't need to be inside a document ready should be in its own file and the same for the portion that needs to be inside a document ready (even if the one script could potentially call a function in the other file)?
thanks
Violet_82
89
Posting Whiz in Training
Recommended Answers
Jump to PostNot sure about best practice, but I put them together so the browser only needs to load a single file.
All 4 Replies
pritaeas
2,179
¯\_(ツ)_/¯
Moderator
Featured Poster
DavidB
44
Junior Poster
JorgeM
958
Problem Solver
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.