Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how do i put all javascript codes in 1 .js file instead of making many .js files for each element also without getting problems in other elements when i put all codes in 1 file?

like in CSS we can open and close like this div1{style here} so we can put many codes as we want in css file. but how we do it in .js ?

Thanks for who answer me.
Posted
Comments
Sergey Alexandrovich Kryukov 3-Feb-15 11:52am    
There is no such problem. Just write it all in one file. I have no idea where did you manage to dig out a problem. Perhaps if you can demonstrate it on a simple sample, I'll answer.
Generally, use document.getElementById for all elements in question, in one file or not, and work with obtained DOM elements.
—SA
Member 10749093 4-Feb-15 1:10am    
thanks for your answer. i just wanted to make sure because, i have 2 pages and on page1.html i have div called load_content that will display div in 10 seconds and load its content after the page loaded. and on page2.html i have div called refresh_in_ten that will appear in 10 seconds and refresh the page automatically. so now on each page in head section i have linked same js file like this <script type="text/javascript" src="main.js"></script>

so now in that main.js file i have code for that div load_content and i have code for div refresh_in_ten

and that make problem for my divs one working and other don't etc. unless i make 2 js files and enter the codes for each div in single file

1 solution

IMHO, use external JavaScript file for those codes that can be re-used across multiple HTML pages. Otherwise, place the JavaScript code in the HTML page to which it applies.
Refer: http://www.w3schools.com/js/js_whereto.asp[^]
 
Share this answer
 
Comments
Member 10749093 4-Feb-15 1:13am    
thanks for your answer peter. the reason i want to put all codes in 1 single file because i want to make my page load faster instead of creating so much js files and linked to my page it will make the page load slow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900