--- title: ウェブページで JavaScript を使う方法 slug: Learn/HTML/Howto/Use_JavaScript_within_a_webpage tags: - Beginner - HTML - JavaScript - OpenPractices translation_of: Learn/HTML/Howto/Use_JavaScript_within_a_webpage ---
Take your webpages to the next level by harnessing JavaScript. Learn in this article how to trigger JavaScript right from your HTML documents.
前提条件: | 基本的な HTML 文書の作成方法を習熟している必要があります。 |
---|---|
目的: | HTMLファイルからJavaScript を起動する方法と、JavaScript にアクセスできるようにする最も良い習慣を学ぶ。 |
{{Glossary("JavaScript")}} はウェブページをインタラクティブにするための、主にクライアントサイドで使われるプログラミング言語です。 JavaScript を使わなくてもすばらしいウェブページを作ることができますが、JavaScript は全く新しいレベルの可能性を開きます。
In this article we're going over the HTML code you need to make JavaScript take effect. If you want to learn JavaScript itself, you can start with our JavaScript basics article. If you already know something about JavaScript or if you have a background with other プログラミング言語s, we suggest you jump directly into our JavaScript Guide.
Within a browser, JavaScript doesn't do anything by itself. You run JavaScript from inside your HTML webpages. To call JavaScript code from within HTML, you need the {{htmlelement("script")}} element. There are two ways to use script
, depending on whether you're linking to an external script or embedding a script right in your webpage.
Usually, you'll be writing scripts in their own .js files. If you want to execute a .js script from your webpage, just use {{HTMLElement ('script')}} with an src
attribute pointing to the script file, using its URL:
<script src="path/to/my/script.js"></script>
Pro tip: In many cases it's a good idea to put your {{HTMLElement('script')}} elements at the end of your HTML document (right before the </body>
closing {{Glossary("tag")}}).
Loading and running scripts is blocking and immediate. That means, every time the browser comes across a <script>
element, the browser stops reading the HTML and instead loads and runs the script. The browser continues reading and rendering the HTML code after running the script.
When you put {{htmlelement("script")}} elements at the end, you don't run the risk of manipulating {{Glossary("DOM")}} nodes that are not yet initialized. In addition, your webpages will finish displaying faster.
You may also add JavaScript code between <script>
tags rather than providing an src
attribute.
<script> window.addEventListener('load', function () { console.log('This function is executed once the page is fully loaded'); }); </script>
That's convenient when you just need a small bit of JavaScript, but if you keep JavaScript in separate files you'll find it easier to
Accessibility is a major issue in any software development. JavaScript can make your website more accessible if you use it wisely, or it can become a disaster if you use scripting without care. To make JavaScript work in your favor, it's worth knowing about certain best practices for adding JavaScript:
<noscript>To use this site, please enable JavaScript.</noscript>
Since almost everybody does have JavaScript enabled, <noscript>
is no excuse for writing inaccessible scripts.