Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ECMAScript Cookbook

You're reading from   ECMAScript Cookbook Over 70 recipes to help you learn the new ECMAScript (ES6/ES8) features and solve common JavaScript problems

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788628174
Length 348 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ross Harrison Ross Harrison
Author Profile Icon Ross Harrison
Ross Harrison
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Building with Modules 2. Staying Compatible with Legacy Browsers FREE CHAPTER 3. Working with Promises 4. Working with async/await and Functions 5. Web Workers, Shared Memory, and Atomics 6. Plain Objects 7. Creating Classes 8. Inheritance and Composition 9. Larger Structures with Design Patterns 10. Working with Arrays 11. Working with Maps and Symbols 12. Working with Sets 13. Other Books You May Enjoy

Creating an HTML page that loads an ECMAScript module

In previous recipes, we went over installation and configurations instructions to run a static file server using Python and configure a browser to use ES modules.

Getting ready

This recipe assumes that you have the static file server running in your working directory. If you haven't installed Python or configured your browser to work with ES modules, please see the first two recipes in the book.

The following steps will demonstrate how to create an ES module and load it into an HTML file.

How to do it...

  1. Create an hello.html file with a some text content:
<html>
<meta charset="UTF-8" />
<head>
</head>
<body>
Open Your Console!
</body>
</html>
  1. Open hello.html by opening your browser, and entering the following URL:  http://localhost:8000/hello.html.
  1. You should see Open Your Console! displayed by the browser:

  1. Lets do what the page tells us and open up the Developer Console. For both Firefox and Chrome, the command is the same:
    • On Windows and Linux:
Ctrl + Shift + I 
  • On macOS:
Cmd + Shift + I  
  1. Next, in the same directory, create a file called hello.js, which exports a function named sayHi that writes a message to the console:
// hello.js 
export function sayHi () { 
  console.log('Hello, World'); 
} 
  1. Next add a script module tag to the head of hello.html that imports the sayHi method from hello.js (pay attention to the type value).
  1. Reload the browser window with the Developer Console open and you should see the hello message displayed as text:

How it works...

Although our browser can work with ES modules, we still need to specify that is how we want our code to be loaded. The older way of including script files uses type="text/javascript". This tells the browser to execute the content of the tag immediately (either from tag contents or from the src attribute).

By specifying type="module", we are telling the browser that this tag is an ES module. The code within this tag can import members from other modules. We imported the function sayHi from the hello module and executed it within that <script> tag. We'll dig into the import and export syntax in the next couple of recipes.

See also

  • Exporting/importing multiple modules for external use
  • Adding fallback script tags
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image