JavaScript Where to Put and output

JavaScript Where to Put and output



Where to Put JavaScript in HTML.


The HTML code in the HTML must be inserted in the right place, if the code is not in its proper place, it could spoil the webpage you created. For this reason it should always be aware of what place the javascript code is put in.

JavaScript Where to Put and output


<Script> tags
Always in HTML javascript, the javascript code is inserted between the <script> (the start tag) and the </ script> (end tag) tags. Remember this when you are writing a JavaScript code for your web page.
The old javascript example that you have learned can use one type of feature: it is something like this <script type = "text / javascript">.
But this type of attribute is not necessary in the HTML. Javascript is the default scripting language in the HTML language.
in javascript <body> tag :
Example-


<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Body tag.</h2>
<p id="io"></p>
<script>
document.getElementById("io").innerHTML = "javascript code always in -_script_tag ";
</script>
</body>
</html>

Result-
JavaScript in Body tag.


JavaScript Functions & Events
A javascript function in HTML is a block of JavaScript code, in which many codes are written, and it can be executed during "called".

For example, when there is an event. Like if we click on the button in any webpage, then we have to activate that function.

You can move any type of script into any HTML document.
You can put javascript in the <body>, or <head> or both sections of any HTML page.



Javascript in <head>
In this example, you will see how a JavaScript function is placed in the <head> section of an HTML page.
In this example, when the button is clicked, the function is called (called):
Example-


<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Paragraph is changed.";
}
</script>
</head>
<body>
<h2>JavaScript in HTML Head tag.</h2>
<p id="demo">A Paragraph webstudy.</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>

Result-
JavaScript in HTML Head tag.
A Paragraph webstudy.



JavaScript output


Javascript display
Javascript can "display" the data in different ways and ways in HTML:
You understand how Javascript does this.


  • Using javascript internal HTML, writes in HTML element.
  • To write JavaScript output, use Document.write ().
  • Javascript uses Window.alert () to write in an alert box.
  • To write to the JavaScript browser console, you have to use Console.log ().
  • Using internal HTML
  • The HTML element is accessed in JavaScript using the .getElementById (id) method.


Javascript ID attribute defines element in HTML. And the internal HTML property defines the content inside the HTML, as you can see in the following example:
Example-


<!DOCTYPE html>
<html>
<body>
<h1>My Web Page</h1>
<p>My  Paragraph for javascript</p>
<p id="as"></p>
<script>
document.getElementById("as").innerHTML = 5 + 6;
</script>
</body>
</html>

Result-
My Web Page
My Paragraph for javascript


Using Document.write () in JavaScript
For testing purposes in JavaScript, document.write () is used and it is convenient:
Example-


<!DOCTYPE html>
<html>
<body>
<h1>My Page webstudy</h1>
<p>My  paragraph for javascript.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>

Result-

My Page webstudy

My paragraph for javascript.


window.alert ()
The alert box in javascript is used to display data to the user in different modal dialogs. You press the OK button to continue this example.
Example-


<!DOCTYPE html>
<html>
<body>
<h2>Web Page</h2>
<p>paragraph.</p>
<script>
window.alert(5 + 6);
</script>
<input type="button" onclick="window.alert(5 + 6);" value="ok"/>
</body>
</html>

Result-
Web Page
paragraph.


console.log ()
Use the console.log () method in JavaScript to display the data in the console. All modern types of browsers come with a built-in console. In the browser, use the F12 key to open the console. Very helpful in testing the console debugging and javascript code in the browser.
Firebug is a debugging tool in the browser that uses the console. In javascript, console.log () is used to print information messages and to display JavaScript errors. Javascript syntax errors are shown in the browser in the console console window.
You can run this example in your HTML code editor.
Example-


<!DOCTYPE html>
<html>
<body>
<h2>Activate debugging with F12</h2>
<p>Select "Console" in the browser debugger menu. Then reload you page.</p>
<script>
console.log(5 + 6);
</script>
</body>
</html>



Where to Put JavaScript in HTML. The HTML code in the HTML must be inserted in the right place, if the code is not in its proper place, it could spoil the webpage you created. For this reason it should always be aware of what place the javascript code is put in.and JavaScript output Javascript display Javascript can "display" the data in different ways and ways in HTML: You understand how Javascript does this.

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget