JavascriptをHTMLで使う方法
外部のJavascriptファイルをindex.htmlで読み込み動作させる。
- フォルダ構成
.
├─js
| └─sample.js
└─index.html
- 実装
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="uft-8">
<script src="js/sample.js" async></script>
<title>HTML入門-JavaScriptの基本</title>
</head>
<body>
<h1>文書の主見出し</h1>
<button type="button" onclick="ShowAlert()">ここをクリック</button>
</body>
</html>
sample.js
function ShowAlert() {
alert("Hello world!!");
}
参考