DelphiFAQ Home Search:

Organizing Javascript in Include Files


commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.


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?


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

// -->

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"; //-->

Content-type: text/html


2012-06-05, 21:29:47
anonymous from China  
<strong> http://www.coachout..strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong> http://www.myloveco..strong> <a href=' http://www.myloveco..g>Coach Outlet Online</strong></a><br />
<strong> http://www.buyincoa..strong> <a href=' http://www.buyincoa..g>Coach Outlet</strong></a><br />
<strong> http://www.cheapcoa..strong> <a href=' http://www.cheapcoa..g>Cheap Coach Handbags</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory Outlet</strong></a><br />
<strong> http://www.buycoach..strong> <a href=' http://www.buycoach..g>Coach Outlet Online</strong></a><br />
<strong></strong> <a href=' http://www.coachout..g>Coach Factory Outlet</strong></a><br />
<strong></strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong></strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong></strong> <a href=' http://www.gotocoac..g>Coach Outlet Online</strong></a><br />
2012-11-12, 01:58:58
anonymous from United Kingdom  



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: