Understanding basic JavaScript codes.

Understanding basic JavaScript codes.

JavaScript is a programming language that adds interactivity to your website (for example games, responses when buttons are pressed or data is entered in forms, dynamic styling, animation). This article helps you get started understanding basic JavaScript codes.

JavaScript is a programming language that adds interactivity to your website (for example games, responses when buttons are pressed or data is entered in forms, dynamic styling, animation). This article helps you get started understanding basic JavaScript codes.

Inserting JavaScript into a webpage is much like inserting any other HTML content. The tags used to add JavaScript in HTML are . The code surrounded by the tags is called a script blog.

``` in the end of tag inside HTML file.

Code for Painting the page light blue

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">
   <p>Paragraph 1</p>
   <script type="text/javascript">
   document.bgColor ="lightblue";
   </script>
  </body>
<html> 

Output:

The colour of web-page is light blue , but the opening body tag is defined to set background colour to be white.

<body bgcolor="white">

The background colour of page is light blue because JavaScript is used to set the document's backround colour to be light blue.

document.bgcolor="lightblue";

Learning from the code:

  1. Page is known as document for the purpose of scripting in a web page.

  2. Properties of the document can be referenced by writing document followed by a dot, followed by the property name. The document has lots of properties.

  3. After comes.

Code to write something to a web page using JavaScript:

Lets write “Hello World!” to a blank page using JavaScript.

DISPLAYING THE RESULT ON THE WEBPAGE.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0
 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
 <html xmlns=”http://www.w3.org/1999/xhtml”>
  <head>
    <title></title>
  </head>
  <body>
   <p id="ResultsP"></p> 
   <script type="text/javascript">//Script Block 1
   document.getElementById('ResultsP').innerHTML ='Hello World!';
   </script>
  </body>
<html> 

Output:

Learning from the code:

  1. The following line has been added to this code.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0
 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
 <html xmlns=”http://www.w3.org/1999/xhtml”>

This lets the web browser know that user is using XHTML. It doesn’t actually make any difference to the code; it would work just fine without the extra lines.

  1. Notice that

    tag has been given an id using the id attribute.

<p id=”ResultsP”>

This id must be unique in the web page, because it is used by the JavaScript to identify the specific HTML element in the following line:

document.getElementById(‘ResultsP’).innerHTML = ‘Hello World!’;

The code simply means: “Get me the document element with id ResultsP and set the HTML inside that element to Hello World!”

It is important that the code accessing the paragraph is after the paragraph otherwise, the code would be attempting to access a paragraph before it existed in the page and would throw an error.

javascript web-development

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Best Web and App Development Company in Ahmedabad

Contact DataPierce for any questions or queries you have about website development, designing, marketing projects or any small or enterprise software development.

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

For World Class Web Development Services in India visit RB Genie

Do you want excellent and world class web development services for your valuable projects? Contact **RB Genie **now, we have more than 8 years experienced team of web developers, which specializes in overall web design and website development...

JavaScript developers should you be using Web Workers?

Do you think JavaScript developers should be making more use of Web Workers to shift execution off of the main thread?