“The World’s Most Misunderstood Programming Language” – Douglas Crockford
It was about my 3rd year in university that I decided I wanted to learn a programming language that would let me develop any kind of software I wanted, be it web apps, mobile apps, video games or desktop apps. I was looking for a language that had such capabilities. My first move was to learn Java because of its Write Once, Run Anywhere (WORA) slogan. I soon came to realize that WORA is a pipe dream (especially with Java). So, after doing a little research I figured out 2 languages that could help me achieve what I wanted (more or less)
So, let’s start with a basic HTML page.
<html> <head> <title>My First JS Program</title> </head> <body> <h1 id="heading">Hello, World!</h1> </body> </html>
Save that as
index.html on your desktop and simply open it with any web browser. You should see the following,
id attribute to the h1 tag.
- Changing the content displayed on the page
- Adding animations
- Interacting with the user
- Receiving user input
- Getting files and content from different servers
- Playing audio and video files
- and much, much more…
To do that we will have to add
<script> tag in our web page.
The best practice for adding JS scripts is before the closing tag of the body. Notice the
Now, let’s add some code:
document I can access and modify any part of the HTML. I use the
.getElementById() method to retrieve the
h1 tag using the
id I defined previously. Now, I am able to modify the heading element however I wish. For now, I simply changed the text from
Hello, World! to
Hello, Osama and colored it red. Let’s see the output!
Getting Input from the User
prompt() function for that.
Now, when you refresh the web page, you will be prompted to enter your name and your favorite color. And the heading content will be dependent upon your inputs, e.g. for the input of “Arshad” and “magenta”, the output would be:
I hope I can share more of my knowledge of JS on this blog soon. Just have to overcome my laziness a bit.