× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to embed an external JavaScript file

Andrew Buchan
Nov 19, 2016
<p>If you have some JavaScript code which you want to run in multipled webpages, do not simply copy and paste the code!</p> <p>Why not?</p> <p>1. Every time a request comes in to your webserver the same JavaScript will be pushed back down to the client, so you are losing bandwidth!</p> <p>2. Maintenance - if you need to update the code, you will need to update it in several places!</p> <p> </p> <p>What do I do instead, I hear you ask!</p> <p>1. Save your JavaScript code in a file named filename.js (feel free to name it something more descriptive)</p> <p>2. From each webpage file you want to include the JavaScript in, add this line</p> <p>&lt;script src="filename.js"&gt;&lt;/script&gt; (remembering to change filename.js to whatever you called it)</p> <p>Final note: Do not include the &lt;script&gt; &lt;/script&gt; tags in the .js file!</p>
comments powered by Disqus