user2142162
user2142162

Reputation: 11

Can't call javascript file from my html page

I'm new to JavaScript and just want to put my JavaScript code in another file.

This is my html page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>my badass page</title>
    <script type="text/javascript" scr = "testing.js"></script>//this contains the function I want to call
</head>
<body id="body">
    <button type="button", onclick="showDate()">show the date</button>
</body>
</html>

This is the testing.js file:

function showDate() {
    alert ("this works")
}

I'm assuming that I just make a beginner mistake because it seems really common but I can't figure it out.

Upvotes: 0

Views: 3049

Answers (3)

slobodan.blazeski
slobodan.blazeski

Reputation: 1040

Quick use of html validator such as http://validator.w3.org/nu/ will uncover lots of problems in your code. Just copy paste and correct the errors.

Upvotes: 0

Nathan van der Werf
Nathan van der Werf

Reputation: 332

change the button to

<button type="button" onclick="showDate()">show the date</button>

and change scr to src

EDIT: source that works:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>my badass page</title>
    <script type="text/javascript" src="testing.js">
    </script>
</head>
<body id="body">
    <button type="button" onclick="showDate();">show the date</button>
</body>
</html>

Upvotes: 3

Mike Corcoran
Mike Corcoran

Reputation: 14565

you spelled the 'src' attribute incorrectly on your tag

you spelled it scr, it should be src

this:

<script type="text/javascript" scr = "testing.js"></script>

should be this:

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

Upvotes: 5

Related Questions