0% found this document useful (0 votes)
25 views17 pages

Java Lab4

This document outlines a lab exercise for JavaScript at Hamdard University, focusing on the basic structure, functions, and syntax of JavaScript. It covers topics such as the <script> tag, manipulating HTML elements, JavaScript variables, functions, and objects, along with practical tasks for students to complete. The lab tasks include creating web pages that demonstrate variable manipulation, user input, and object creation.

Uploaded by

laibazaheer1111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
25 views17 pages

Java Lab4

This document outlines a lab exercise for JavaScript at Hamdard University, focusing on the basic structure, functions, and syntax of JavaScript. It covers topics such as the <script> tag, manipulating HTML elements, JavaScript variables, functions, and objects, along with practical tasks for students to complete. The lab tasks include creating web pages that demonstrate variable manipulation, user input, and object creation.

Uploaded by

laibazaheer1111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University Lab #04 JavaScript Objectives: * To lear the basic structure of JavaScript. * To learn the functions of Javascript Theory: What is JavaScript? * JavaScript and Java are two different computer languages. * [tis an object-oriented computer programming language commonly used to create interactive effects within web browsers. * JavaScript, is a text based object oriented computer programming language meant to run as part of a web-based application and to create interactive effects within web browsers. ‘* One of the three pillars of web development—the other two being HTML and Css. The JavaScript in or You can place an unlimited number of scripts in an HTML document. Scripts can be in the or in the section of HTML, and/or in both. It is a common practice to put functions in the section, or at the bottom of the page. This way they are all in one place and do not interfere with page content. JavaScript in In this example, JavaScript writes into the HTML while the page loads FACULTY OF ENGINEERING St Hamdard ») ) od SCIENCES AND TECHNOLOGY University

JavaScript can write directly into the HTML output stream:

You can only use [Link] in the HTML output. If you use it after the document has loaded (e.g. in a function), the whole document will be overwritten. JavaScript can write directly into the HTML output stream: This is a heading This is @ paragraph You can only use [Link] in the HTML output. If you use it after the document has loaded (e.g. in a function), the whole document will be overwritten. External JavaScripts Scripts can also be placed in external files. Extemal files often contain code to be used FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University by several different web [Link] JavaScript files have the file extension .[Link] use an external script, point to the js file in the "src" attribute of the My First Web Page My First Paragraph. After click Oops! The document disappeared! JavaScript Code JavaScript code (or just JavaScript) is a sequence of JavaScript statements. * Each statement is executed by the browser in the sequence they are written © This example will manipulate two HTML elements: ff (\\ a) we FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University My Web Page Hello Dolly How are you?

My Web Page

‘demo>A Paragraph. spit yDIV">A DIV. JavaScript Functions Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon) The function can be called directly when an event occurs (like when a user clicks a button), and it can be called from "anywhere" by JavaScript code FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University
shead> Try ite/button> Click the button to call a function with arguments Example:

This example calls a function which performs 2 calculation, and retums the result:

FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University while loop Do while loop i? (\\\ @y er FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University JavaScript Functions The function can be called directly when an event occurs (like when a user clicks a button), and it can be called from "anywhere" by JavaScript code.
shead> ‘myFunction()'>Try ite/button> Declare the argument, as variables, when you declare the function: function myFunction(varl,var2) { //some code } The variables and the arguments must be in the expected order. The first variable is given the value of the first passed argument etc. <(DOCTYPE htmi> Click the button to call a function with arguments

This example calls a function which performs a calculation, and retums the result:

This example calls a function which performs a calculation, and returns the result 12 a) ‘ae er FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University JavaScript Objects "Everything’ in JavaScript is an Object: a String, a Number, an Array, a Date. In JavaScript, an object is data, with properties and methods. Creating JavaScript Objects Almost "everything" in JavaScript is an object. Strings, Dates, Arrays, Functions. You can also create your ovin objects This example creates an object called "person’, and adds four properties to it: Johnis 50 years old. Accessing Object Properties The syntax for accessing the property of an objectis: a WY Se FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University [Link] FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY Hamdard University Lab Tasks: Q1. Create a webpage that declaresa variable, assigns a value to it, displays the value, change the value, and displays the value again and print the values on the webpage() Q2. Create JS code to swap two values using third variable. And also without using third variable. Q3. Create a webpage that inputs the name of the user using prompt box, welcomes the user when the page is loaded, tells the length of the name entered and convert the name to the upper case. Hint : to get input from user use the following: var text = prompt(Enter your name ‘Name’ ): alert("Welcome '+ text); Q4, Create an object Employee which has the following properties, id, name, designation, salary, phone number and [Link] a webpage that inputs the data and on clicking the submit button display the data in a dialogue box. Note: Attach a snap shot of every above mentioned task. Learning outcome:

You might also like