7

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!

6
  • Is your add_contact defined inside another block? Commented Jan 13, 2015 at 19:01
  • 11
    For a first type should be text/javascript Commented Jan 13, 2015 at 19:02
  • 4
    @JamesMcDonnell: Good catch on that! Actually the type attribute can just be removed. :-) Commented Jan 13, 2015 at 19:02
  • @RocketHazmat No, the function is the very first line in the .js file Commented Jan 13, 2015 at 19:03
  • 2
    Actually, @JamesMcDonnell that seems to have fixed it! Thank you very much :) Commented Jan 13, 2015 at 19:04

2 Answers 2

7

Type should be text/javascript not javascript/text

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

Comments

3

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>

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.