General

JavaScript examples

The web programming is that activity in which a software developer creates web sites of any kind. The use of one or another programming language lies in the objectives to be achieved, but there are some languages ​​that are fundamental in programming and web design, such is the case of JavaScript.

What is JavaScript?

JavaScript is an interpreted programming language that is used to make a web page more complex , that is, actions can be taken instead of just displaying text or static images.

Basically, JavaScript is used to make the HTML code that is, let’s say, the ‘skeleton’ of a website more dynamic. With JS, you can update information, make people interact on the page, give images movement, and more.

JavaScript examples

  • Example 1: Line of code to call the JS and make it work inside the page

<script src = »scripts / main.js»> </script>

  • Example 2: add a function to a button previously created with HTML and CSS

const for = document.querySelector (‘p’);

para.addEventListener (‘click’, updateName);

function updateName () {

let name = prompt (‘Please enter a new name’);

para.textContent = ‘Player 1:’ + name;

}

  • Example 3: Adding a welcome message

<LANGUAGE script = ”JavaScript”>

Alert (“Welcome to the site of…!”);

</script>

  • Example 4: Button

<input type = ”button” onClick = »history.back ()» value = ”Back button”>

  • Example 5: Full code with HMTL and JS for a boxed menu

<! DOCTYPE html PUBLIC «- // W3 C // DTD XHTML 1.0 Transitional // EN» «https: //www.w3.org/TR/xhtml1/ DTD / xhtml1-transitional.dtd»>

<html xmlns = »https://www.w3.org/1999 / xhtml»>

<head>

<meta http-equiv = »Content-Type» content = »text / html; charset = utf-8 ″ />

<title> Untitled Document </title>

<script type = »text / javascript»>

<! –

function MM_jumpMenu (targ, selObj, restore) {//v3.0

eval (targ + ». location = ‘» + selObj.opti ons [selObj.selectedIndex] .value + »’»);

if (restore) selObj.selectedIndex = 0;

}

</script>

</head>

<body>

<form name = »form» id = »form»>

<select name = »jumpMenu» id = »jumpMenu» onchange = »MM_jumpMenu (‘parent’, this, 0)»>

<option value = »menu.html»> Menu1 </option>

<option value = »menu2.html»> Menu2 </option>

<option value = »menu3.html»> Menu3 </option>

</select>

</form>

</body>

</html>

  • Example 6: Block comment in JavaScript
  • <html> <body> <script type = »text / javascript»> / *This is a comment

    per block. No matter

    the number of lines.

    * /

    document.write (“<h1> What is commented, is not written </h1>”);

    </script>

    </body>

    </html>

  • Example 7: Header

<! DOCTYPE html>
<html>
<head>
<script>
function myFunction () {
document.getElementById (“demo”). InnerHTML = “Paragraph changed.”;
}
</script>
</head>
<body>

<h2> JavaScript in Head </h2>

<p id = »demo»> A Paragraph. </p>

<button type = »button» onclick = »myFunction ()»> Try it </button>

</body>
</html>

  • Example 8: Using a file

<body>

<h2> External JavaScript </h2>

<p id = »demo»> A Paragraph. </p>

<button type = »button» onclick = »myFunction ()»> Try it </button>

<p> (myFunction is stored in an external file called «myScript.js») </p>

<script src = »myScript.js»> </script>

</body>
</html>

Related Articles

Leave a Reply

Your email address will not be published.

Check Also
Close