User Tools

Site Tools


java-script:hello-world

This is an old revision of the document!


Java Script Hello World

The following script will display 'Hello World' both in a popup window and in the browser's console. To execute the script, it must be run within a web browser.

You can view the console output by accessing the browser's developer tools.
hello-world-js.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
</head>
<body>
    <h1>Hello World! from JS in a script tag</h1>
    <script>
        alert('Hello world!');
        const h1 = document.querySelector('h1');
        console.log(h1.textContent);
    </script>
</body>
</html>
In modern HTML (specifically, HTML5), it's no longer necessary to specify the type attribute when using JavaScript within a <script> tag. Browsers that support HTML5 will default to JavaScript if the type attribute is omitted.

In previous versions of HTML, it was common to see the type attribute set like this:

<script type="text/javascript">
  // Your JavaScript code here
</script>

However, since HTML5, this is considered unnecessary, and the following is completely acceptable and standard:

<script>
  // Your JavaScript code here
</script>

If you're writing HTML that must conform to older standards or you want to be explicitly clear about the type, you can still include the type attribute. However, for most modern web development, it can safely be omitted.

The lang attribute in the <html> tag doesn't default to “en” or any other language value. If it's not specified, browsers and other user agents have no definitive way to know the language of the document.

Omitting the lang attribute might not cause any immediate or apparent issues, especially if the content is in English, since many tools and systems may assume English as a default. However, it's still considered best practice to include the lang attribute to explicitly declare the language.

This can be particularly important for accessibility tools like screen readers, which use the language information to determine the correct pronunciation rules. It's also useful for search engines, which may use the language information to serve the page to the right audience.

In summary, while many systems might assume English as the default language if the lang attribute is omitted, it's still recommended to include it to ensure that your HTML is semantically correct and accessible.

java-script/hello-world.1691522464.txt.gz · Last modified: 2023/08/08 22:21 by odefta