How To link a JavaScript file in HTML


How do you link a JavaScript file in HTML?

This is 1 problem most bloggers or website owners are facing today but thanks to the group of experts and developers that helped us with the right answers
Code, Html, Digital, Coding, Web 

6 Answers

1.  Rajendra Kumar Vankadari, UI Developer @ Flipkart ex Sapient.
use a script tag.
<script type=’text/javascript’ src=’PATH OF FILE’></script>
This instructs browser that whatever (text) comes within this tag should be treated as a Javascript code rather than plain HTML. Hence it stores it in engine memory and executes it.
However browsers these days are smart enough will understand the text inside this tag as code even if type=”text/javascript” is not mentioned.

2. Eoin Healy, Website Developer (2009-present).
There are 2 ways to do this, 

1) linking to a .js file <srcipt src=”ajavascriptfile.js”></script> (usually within the <head></head> tags

or 2) embeding the js code within the html file like this:

<script type=”text/javascript”>
your js code here

If you can be a bit more specific, I can tell you which is the best option for you.

3. Scott Warner.
You could put something like this in the <head>:

<script src=”FileName.js”></script>

4. Anonymous.
On the bottom of webpage add:

<script src=”directory/filename.js” type=”text/javascript”></script>

Always remember to add the JS file in the bottom. While loading the page, this will help. All the elements will be loaded first and then javascript will be loaded.

5. Bhavesh Nigam, Every hour, happy hour. life is wacky now.    
<script src=”path/to/your/static/js/file”></script>

6. Mark Hay.
I don’t quite know what you’re asking. If you want to include a JS script in an HTML file, you do it like so:

<script src=”yourscript.js”></script>

Let us know if there is another way to insert JavaScript in your own opinion Lets discuss in the comment box 






Was this post helpful?



Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.