| Programming C# C++ (7) Delphi (618) Java (8) JavaScript (30) Document (8) Events (8) ExtJS (8) Strings (3) perl (9) php (4) VBScript (1) Visual Basic (1)
|
Organizing Javascript in Include Files
Question: I use javascript code on pages on my web site, most pages share the code and I need to update the code. How can I organize my site to make this update easier?Answer: If your javascript code is embedded in each html page then you need to do a search and replace across all html files. The first code snippet in the box below shows this scenario.The second snippet assumes that you have a separate file named 'shared_code.js' in your www root directory and each participating HTML file just includes this file. That way you need to change only one file (/shared_code.js) This include file does not need the <script> tags used within the original html document. The file name ending in '.js' tells the browser that the contents will be javascript. You do need to put the <!- ... //--> around the code though for browsers that don't understand javascript in case a user directly opens your .js file. The third part in the box shows how your include file should look like:
Comments:
|