How can you use html tags in javascript?

Back to Class 9 page »

Including JavaScript in your page is a fairly simple process.

First, What Is JavaScript?

From the W3C Schools Site:

  • JavaScript was designed to add interactivity to HTML pages
  • JavaScript is a scripting language
  • A scripting language is a lightweight programming language
  • JavaScript is usually embedded directly into HTML pages
  • JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
  • Everyone can use JavaScript without purchasing a license

Including the JavaScript

You can include JavaScript in your HTML in two ways:

  • Writing the code in your HTML
  • Including it as a link to an external file

For the most part, you will include the JavaScript as an external file.

The Script Tag

The <script> tag is what we use to includes our JavaScript. It's a lot like the <link> tag you've already been using to include your CSS files.

Here's a very basic snippet of JavaScript using the script tag. This JavaScript is written directly into our HTML page. It will call and alert box as soon as the page loads.

<script type="text/javascript">
  alert("This alert box was called with the onload event");
</script>

When using the script tag, we must always use the attribute name and value of type="text/javascript".

Using the script tag to include an external JavaScript file

To include an external JavaScript file, we can use the script tag with the attribute src. You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.

<script type="text/javascript" src="path-to-javascript-file.js"></script>

This script tag should be included between the <head> tags in your HTML document.

JavaScript Files

JavaScript files are not HTML files or CSS files.
  • Always end with the js extension
  • Only include JavaScript

It's customary to put all JavaScript files in a folder called js on websites, like so:

How can you use html tags in javascript?

Simple Demo of Including JavaScript

Here's a very simple demonstration of how to include an external JavaScript file into an HTML page.

  • Basic JavaScript Example

Other People's JavaScript

For this class you are not expected to write any actual JavaScript code. Lucky for you, many of people have already written lots of JavaScript and even allow you to use it for free.

JavaScript Frameworks

A Framework is basically a library of code for a certain language. Generally, the framework abstracts common tasks and makes it easier and faster for designers and developers to write their specific code. Frameworks don't really do anything by themselves, they just provide an easier platform for people to build on.

Common JavaScript Frameworks:

  • JQuery
  • Prototype
  • MooTools
  • script.aculo.us

Of these frameworks, JQuery is currently the most popular one. It's also the one you're most like to encounter being used as a buzz word.

JavaScript File Sizes

Many JavaScript files can tend to be rather large, which can slow down the load time of your page. Popular frameworks usually offer a "minified" version of their code. You should always use this version in your pages because it will have a smaller file size.

Back to Class 9 page »

Topic: JavaScript / jQueryPrev|Next

Answer: Use the concatenation operator (+)

The simple and safest way to use the concatenation operator (+) to assign or store a bock of HTML code in a JavaScript variable. You should use the single-quotes while stingify the HTML code block, it would make easier to preserve the double-quotes in the actual HTML code.

You also need to escape the single-quotes which appears inside the content of HTML block — just replace the ' character with \', as shown in the following example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Store HTML Code in JavaScript Variable</title> 
</head> 
<body>
<div id="wrapper"></div>
<script>
// Storing HTML code block in a variable
var codeBlock = '<div class="content">' +
                    '<h2>This is a heading</h2>' +
                    '<p>This is a paragraph of text.</p>' +
                    '<p><strong>Note:</strong> If you don\'t escape "quotes" properly, it will not work.</p>' +
                '</div>';

// Inserting the code block to wrapper element
document.getElementById("wrapper").innerHTML = codeBlock
</script> 
</body>
</html>


Here are some more FAQ related to this topic:

  • How to add elements to DOM in jQuery
  • How to remove wrapper element but keep the text content intact using jQuery
  • How to bind click event to dynamically added HTML elements in jQuery

How does HTML work with JavaScript?

javascript is embedded into html with script tags. if you open a raw javascript file the browser won't execute it, it will just show the code. ... .
HTML provides structure and semantics like XML, CSS provides presentation, colors, fonts etc..

HOW include HTML code in JavaScript?

How TO - Include HTML.
The HTML. Save the HTML you want to include in an .html file: content.html. ... .
Include the HTML. Including HTML is done by using a w3-include-html attribute: Example. ... .
Add the JavaScript. HTML includes are done by JavaScript. Example. ... .
Include Many HTML Snippets. You can include any number of HTML snippets:.