Intro to JavaScript
Zenva
ACCESS the FULL COURSE here: https://academy.zenva.com/product/bite-sized-coding-academy/
TRANSCRIPT
In this lesson, we'll create a hello world example in JavaScript. I'll teach you how to include Javascript code both inside a HTML document and in external files. We're going to start by opening up our code editor. Lets create a new document and save it as index.html. Now, lets open this index.html file in our web browser. I'll be using Chromium which is the open source version of Google Chrome. You can use any web browser that you want for this course. As long as it's a current web browser. So I'm going to open this up in Chromium. We're going to type in some basic HTML code. So I'm going to start by typing in HTML tag. So I'm adding in a head and a body. Inside the body, I'm just going to type in hello. And there we go. Now what happens if I just type in Javascript code here? I'm gonna type in alert("Hello World"), which is the code to render a message box that says hello world. But as you can see, if I refresh this page, this is being shown as text. Now, how can we make the browser execute, or run, this line of code? What we can do is use the script tag. So by using the script tag, you can enter javascript code in your HTML documents. Everything that I type inside of the script tag, is treated as javascript code. Lets reload the page again and see what happens now. See how now I get the "Hello World" message on my message box, just like we wanted. Writing code inside of a HTML document, like what we're doing here, is fine. But what if you wanted to include the same javascript code in multiple HTML files? Well the best way to do that is by creating an external javascript file. So lets create a new file and save it as script.js. We're going to cut and paste the javascript code inside of this new file, and save. I'll be adding another, an attribute here called source, src, and then double quotes. And then I can specify the name of that external file. So if I reload the page now, you'll see that it works in the exact same way. Because we're basically including the code of this external file into the HTML document. What happens if a had another folder here, for instance, called js and this script file was inside of that folder? How can we include it in that case? All you have to do is add the path to the location of that file in here. The folder's called js. So I will type in js and then forward slash, and the name of the file.
Free Tutorials:
- Unity: https://gamedevacademy.org
- Phaser: https://phasertutorials.com
- Machine Learning: https://pythonmachinelearning.pro
- Web Dev: https://html5hive.org
- Android: https://androidkennel.org
- Swift: https://swiftludus.org
- VR: https://vrgamedevelopment.pro ... https://www.youtube.com/watch?v=TKX-NodRVAI
6595020 Bytes