DelphiFAQ Home Search:
General :: Web publishing :: JavaScript
Code snippets to add functionality to your HTML pages, preferably cross-browser.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

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:

The following Javascript is embedded directly in the HTML pages:
 
 <script language="javascript" type="text/javascript">
 <!--
 
 your script here
 
 // -->
 </script>
 
 
Better to do it this way in your HTML file: <script src="/shared_code.js"></script>
and this is /shared_code.js: <!-- var days=new Array(7); days[1]="Monday"; days[2]="Tuesday"; days[3]="Wednesday"; //-->

Generated 20:02:14 on Sep 19, 2017