最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can't call javascript file from my html page - Stack Overflow

programmeradmin1浏览0评论

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 mon but I can't figure it out.

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 mon but I can't figure it out.

Share edited May 17, 2013 at 19:54 MarioDS 13.1k15 gold badges70 silver badges123 bronze badges asked May 17, 2013 at 19:47 user2142162user2142162 111 silver badge3 bronze badges 1
  • I think you just have to format your code properly. scr? type="button", <= a ma?. Etc... – elclanrs Commented May 17, 2013 at 19:49
Add a ment  | 

3 Answers 3

Reset to default 5

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>

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>

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

发布评论

评论列表(0)

  1. 暂无评论