Place JavaScript In Separate Files

Place JavaScript In Separate Files


I want to create a library of generic JavaScript functions. Is it possible to write different functions in different files, and use the functions within other files by “including” these files? Like in C, you can use the #include directive to include other functions.


Yes, you can put your JavaScript into files that you reference from multiple pages. You should make sure that your JavaScript code files use the .js extension. In addition, your shared code library should be stored in a directory on your Web server that is accessible over the Web, just like an image. The syntax for including your code is:

Make sure to include the trailing tag.


Share the Post: