External JavaScript function is undefined but exists

I have 2 files, one HTML, one JavaScript.

JavaScript file (contacts.js):

function add_contact() {
    // Rest of Code here
}

      

HTML file:

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

      

I have a button that, when clicked, calls the add_contact function:

<button type="button" class="btn btn-primary" onclick="add_contact();">Save changes</button>

      

When the JavaScript code is the header of the HTML file, the function works fine. However, it is now part of an external file, it returns the following error:

Unconnect ReferenceError: add_contact is undefined

I feel a little lost with this, so any help would be fantastic!

+3


source to share


2 answers


The type must text/javascript

not bejavascript/text



+4


source


Modify this block of markup:

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

      



in

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

      

+4


source







All Articles