loading

JS Output

JavaScript Display Possibilities

JavaScript is a can “display” data in multiple ways: 

  •   Writing into an HTML a component, employing innerHTML.
  •   Writing into the HTML output using document.write().
  •  Writing into a warning box, use window.alert().
  •   Writing into the viewer console, using console.log().

Using innerHTML

To get an HTML element, JavaScript can use the document.getElementById(id) method.

The id variable gives the HTML element. The innerHTML field provides the HTML content:

Example

				
					<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My First Paragraph</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

<script>class RocketElementorAnimation{constructor(){this.deviceMode=document.createElement("span"),this.deviceMode.id="elementor-device-mode",this.deviceMode.setAttribute("class","elementor-screen-only"),document.body.appendChild(this.deviceMode)}_detectAnimations(){let t=getComputedStyle(this.deviceMode,":after").content.replace(/"/g,"");this.animationSettingKeys=this._listAnimationSettingsKeys(t),document.querySelectorAll(".elementor-invisible[data-settings]").forEach(t=>{const e=t.getBoundingClientRect();if(e.bottom>=0&&e.top<=window.innerHeight)try{this._animateElement(t)}catch(t){}})}_animateElement(t){const e=JSON.parse(t.dataset.settings),i=e._animation_delay||e.animation_delay||0,n=e[this.animationSettingKeys.find(t=>e[t])];if("none"===n)return void t.classList.remove("elementor-invisible");t.classList.remove(n),this.currentAnimation&&t.classList.remove(this.currentAnimation),this.currentAnimation=n;let s=setTimeout(()=>{t.classList.remove("elementor-invisible"),t.classList.add("animated",n),this._removeAnimationSettings(t,e)},i);window.addEventListener("rocket-startLoading",function(){clearTimeout(s)})}_listAnimationSettingsKeys(t="mobile"){const e=[""];switch(t){case"mobile":e.unshift("_mobile");case"tablet":e.unshift("_tablet");case"desktop":e.unshift("_desktop")}const i=[];return["animation","_animation"].forEach(t=>{e.forEach(e=>{i.push(t+e)})}),i}_removeAnimationSettings(t,e){this._listAnimationSettingsKeys().forEach(t=>delete e[t]),t.dataset.settings=JSON.stringify(e)}static run(){const t=new RocketElementorAnimation;requestAnimationFrame(t._detectAnimations.bind(t))}}document.addEventListener("DOMContentLoaded",RocketElementorAnimation.run);</script></body>
</html>
				
			

Changing an HTML element’s innerHTML attribute is a standard method for displaying data in HTML.

Using document.write()

For tests uses, it is simple to use document.write():

Example

				
					<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
document.write(5 + 6);
</script>

</body>
</html>
				
			

Example

------Example mukavu------

The document.write() method must only be used for development.

Using window.alert()

An alert box can be used to show data:

Example

				
					<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
window.alert(5 + 6);
</script>

</body>
</html>
				
			

The window keyword need not be used.

The window object in JavaScript is an international scope object. This indicates that the window object is the usual owner of variables, properties, and methods. This implies that it’s not necessary to specify the window keyword:

Example

				
					<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
alert(5 + 6);
</script>

</body>
</html>
				
			

Using console.log()

You can display data by calling the console.log() method in the browser for research purposes.

A later chapter will cover more about programming.

Example

				
					<!DOCTYPE html>
<html>
<body>

<script>
console.log(5 + 6);
</script>

</body>
</html>
				
			

JavaScript Print

There are no print methods or objects in JavaScript.

JavaScript does not allow you to access output devices.

The window can be called, but that’s just one limitation. To print the contents of the current window.print() method in your browser.

Example

				
					<!DOCTYPE html>
<html>
<body>

<button onclick="window.print()">Print this page</button>

</body>
</html>
				
			
Share this Doc

JS Output

Or copy link

Explore Topic