Parker1090
Parker1090

Reputation: 489

External JavaScript function 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 it's clicked, it calls the 'add_contact' function:

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

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

Uncaught ReferenceError: add_contact is not defined

I feel a bit lost over this one, so any help would be fantastic!

Upvotes: 7

Views: 4919

Answers (2)

James McDonnell
James McDonnell

Reputation: 3710

Type should be text/javascript not javascript/text

Upvotes: 7

Adrian Salazar
Adrian Salazar

Reputation: 5319

Change this block of markup:

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

to:

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

Upvotes: 3

Related Questions