Skip to main content

Chapter 4 Placement of JavaScript - Technology369kk

PLACEMENT

There is a flexiblity given to include Javascript code anywhere in an HTML document. However the most prefrred ways to include JavaScript in an HTML file are as follow: So, In this post we are discuss about that placement  in JavaScript so lets begin. 

  • Script in <head>.......</head> section.
  • Script in <body>.......</body> section.
  • Script in <body>.......</body>  and <head>.....</head> section.
  • Script the following secion, we will see how we can place JavaScript in an HTML file in diffrent ways. 

JavaScript in <head>.........</head> Section: 

  • If you want to have a script run on some event, such as when a user clicks somewhere, then you will place that in the head  as follows. 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript of Placement</title>
 
    <script>
        function sayHello(){
        alert("Hello World")
    }
    </script>

</head>
<body
    <h2>Chapter 4:  PLACEMENTS:</h2>

    <h2>JavaScript in <head>.......</head> Section</h2>
    Click here for the results:
    <input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html>


This code will be produce the following results

Click here for the results: 


JavaScript in <body>.........</body> Section: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript of Placement</title>
</head>
<body>    
    <h2>JavaScript in <body>.......</body> Section</h2>
    Click here for the results:
    <script>
        function clcikConfirm(){
        confirm("I hope u r understand, which place is good?");
    }
    </script>
    <input type="button" onclick="clcikConfirm()" value="ClickHere" />

</body>
</html>


  • Output: This is the following results: 
  • Click here for the results: 


JavaScript in <body> and <head> Sections:

  • You can aslo write javascript code in head and body section altogether as follows: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS Chapter 4_Placement</title>
    <script type="text/javascript">
   
    function SayWishes(){
        alert("Do you want to Learning next page");
    }
    </script>
</head>
<body>    
    <h2>JavaScript in <body>.......<head> Section</h2>
        <script>
            document.log("Do you want next learning topic ");            
        </script>
    <input type="button" onclick="SayWishes()" value="Say Wishes"/>

</body>
</html>


Output: 

Click here for the results:    

JavaScript in External File: 

  • As you begin to work more extensively with javaScript, you will be likely to find that there are cases where you are reusing identical javaScript code on multiple pages of a site. 
  • You are not restricted to be maintainig idetical JavaScript in an external file and then include it your HTML files.
  • Here is an example to show how can include an external JavaScript file in Your HTML code using script tag its src attribute. 
  • Type this code in your html file: 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chapter 4 PLACEMENTS</title>
        <script type="text/javascript" src="testfile.js"></script>

</head>
<body>
    <h2>In this program we are learn about external file how to use</h2>

</body>
</html>


  • To use JavaScript from an external file source, you need to write all your JavaScript source code in a simple text file with the extension ".js" and then include that file as shown above. 
  • For example, you can keeep the following content in test.js file  in you editor folder anf the use can use another file I mean html file after including the testfile.js file. 
  • Type the following content in you .js file. 

function exterFile(){
    alert("Welcome to External File Program in JS");
}
document.write("This is External File..");



The Follwing Output:

This is External File..

In this program we are learn about external file how to use




I hope this is helpful






Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...