48

I have the following two javascript functions:

1

showCountry()

2

showUser()

I would like to put them in external ".js" files

1

<a href="javascript:showCountry('countryCode')">countryCode</a>

2

<form>
 <select name="users" onChange="showUser(this.value)">
 <option value="1">Tom</option>
 <option value="2">Bob</option>
 <option value="3">Joe</option>
 </select>
</form>

What is the correct syntax to call these functions?

6
  • 3
    Create a separate js file in which put these functions or whatever code you want to write and mention src in your script tag like this <script type='text/javascript' language='javascript' src='Path to your external js file'/>. Commented Jul 16, 2012 at 4:31
  • 2
    you should load the external js file before calling function. Commented Jul 16, 2012 at 4:32
  • 1
    document.getElementById('users').addEventListener('change', showUser, false); in a separate JavaScript file would be better than onchange. Commented Jul 16, 2012 at 6:00
  • Note: Point to the external script file exactly where you would have written the script. w3schools.com - HTML <script> src Attribute Commented Jun 25, 2017 at 14:24
  • 3
    I know this is an old post(5+ years), but being a top result in Google I thought it necessary to add this. According to W3Schools you reference the script where you would normally write the script, according to others you reference it in head, but this can cause issues. I've read and found very useful to reference the external script at the bottom of the document right before the body end tag. This allows the entire contents of body to be loaded before running any scripts, thereby preventing a script to be run on an element(s) that don't yet 'exist'. Commented Sep 17, 2017 at 0:36

6 Answers 6

58

Code like this

 <html>
    <head>
          <script type="text/javascript" src="path/to/script.js"></script>
          <!--other script and also external css included over here-->
    </head>
    <body>
        <form>
            <select name="users" onChange="showUser(this.value)">
               <option value="1">Tom</option>
               <option value="2">Bob</option>
               <option value="3">Joe</option>
            </select>
        </form>
    </body>
    </html>

I hope it will help you.... thanks

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you all for replying to me. Thank you Jalpesh Patel for your quick and effectively solution.
Probably changed now, but i put the <script src="aiosdh.js"></script> at the bottom of the body. Anywhere else and it didn't seem to work ?
@nora Can you please put your code on SO? I think there is some mistake in code.
@Jalpesh pastebin.com/dcjiRPDL I stuck it in a paste bin, as it probs wouldn't work well in this comment.
@nora is your issue is fixed??
|
21

Note :- Do not use script tag in external JavaScript file.

<html>
<head>

</head>
<body>
    <p id="cn"> Click on the button to change the light button</p>
    <button type="button" onclick="changefont()">Click</button>

     <script src="external.js"></script>
</body>

External Java Script file:-

        function changefont()
            {

                var x = document.getElementById("cn");
                x.style.fontSize = "25px";           
                x.style.color = "red"; 
            }

Comments

6

In your head element add

<script type="text/javascript" src="myscript.js"></script>

1 Comment

.. and otherwise the usage in your html files is unchanged. However, you can change to "unobtrusive javascript" style - where functionality is attached to your html within your script, rather than inline in your html. en.wikipedia.org/wiki/Unobtrusive_JavaScript
4

This is the way to include an external javascript file to you HTML markup.

<script type="text/javascript" src="/js/external-javascript.js"></script>

Where external-javascript.js is the external file to be included. Make sure the path and the file name are correct while you including it.

<a href="javascript:showCountry('countryCode')">countryCode</a>

The above mentioned method is correct for anchor tags and will work perfectly. But for other elements you should specify the event explicitly.

Example:

<select name="users" onChange="showUser(this.value)">

Thanks, XmindZ

1 Comment

The issue about <a> tags has been discussed extensively here and here. Even though it works, it's not recommended to put Javascript in href.
3

You can simply add your JavaScript in body segment like this:

<body>

<script src="myScript.js"> </script>
</body>

myScript will be the file name for your JavaScript. Just write the code and enjoy!

1 Comment

Welcome to StackOverflow. Please be more specific and describe how to include the JavaScript file into HTML.
2

I hope this helps someone here: I encountered an issue where I needed to use JavaScript to manipulate some dynamically generated elements. After including the code to my external .js file which I had referenced to between the <script> </script> tags at the head section and it was working perfectly, nothing worked again from the script.Tried using developer tool on FF and it returned null value for the variable holding the new element. I decided to move my script tag to the bottom of the html file just before the </body> tag and bingo every part of the script started to respond fine again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.