Running in Browsers

To compile and merge scripts, to eliminate unused code, and to generate smaller JavaScript files, we typically use tools like WebPack. It’s common to use such tools when using Kotlin for the front end as well. Since we’re creating a very small example and our focus isn’t on the front-end development, we won’t delve into that here. Let’s focus on running the greet.js file that we compiled from Kotlin in a browser.

To see the file greet.js run within a browser, we will first need to create an index.html file:

 <!DOCTYPE html>
 <html>
  <body>
  <p>Please see the output in the browser console</p>
  </body>
  <script src=​"./lib/kotlin.js"​></script>
  <script src=​"greet.js"​></script>
 </html>

In the HTML file, remember to load the kotlin.js script first, before loading the greet.js file.

Then open the file index.html within your favorite browser and take a peek at the browser’s console. Here’s an excerpt of the file being executed within Chrome.

images/kotlinjs.png
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset