[go: up one dir, main page]

0% found this document useful (0 votes)
19 views23 pages

User Id: Password

Uploaded by

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

User Id: Password

Uploaded by

luxurypointhub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Here are the answers to your questions:

### **1. Write a Javascript to design a form to accept values for user ID & password:**

```html

<form>

User ID: <input type=”text” id=”userId”><br>

Password: <input type=”password” id=”password”><br>

<input type=”submit” value=”Submit”>

</form>

```

### **2. State any two properties and methods of location object:**

- **Properties:**

1. `location.href` – Gets or sets the entire URL of the current page.

2. `location.hostname` – Returns the domain name of the web host.

- **Methods:**

1. `location.reload()` – Reloads the current page.

2. `location.assign()` – Loads a new document.

### **3. Enlist & explain the use of any two Intrinsic JavaScript Functions:**

1. **parseInt()**: Converts a string to an integer.

```js

Let number = parseInt(“10”); // Returns 10

```
2. **isNaN()**: Checks if a value is NaN (Not a Number).

```js

Let result = isNaN(“Hello”); // Returns true

```

### **4. Describe browser location object:**

The **location object** contains information about the current URL and provides methods to
manipulate the browser location. It includes properties such as `href`, `protocol`, `hostname`, and
methods such as `reload()`, `assign()` to navigate or refresh the browser page.

### **5. Enlist any four mouse events with their use:**

1. **onclick** – Triggered when an element is clicked.

2. **onmouseover** – Triggered when the mouse pointer moves over an element.

3. **ondblclick** – Triggered when an element is double-clicked.

4. **onmousedown** – Triggered when the mouse button is pressed down on an element.

### **6. Differentiate between session cookies and persistent cookies:**

- **Session cookies**: These are temporary cookies stored in the browser’s memory and deleted once
the browser is closed.

- **Persistent cookies**: These cookies remain on the user’s device for a specified time and can be used
to track user behavior across multiple sessions.

### **7. Explain the following form events:**

- **onmouseup**: Triggered when the mouse button is released over an element.

- **onblur**: Triggered when an element loses focus (e.g., when moving away from an input field).
### **8. Write a JavaScript program to change the contents of a window:**

```html

<button onclick=”changeContent()”>Change Content</button>

<p id=”content”>This is the original content.</p>

<script>

Function changeContent() {

Document.getElementById(“content”).innerHTML = “The content has been changed!”;

</script>

```

### **9. Write a JavaScript syntax for accessing elements of another child window:**

```js

Let childWindow = window.open(“child.html”, “_blank”);

childWindow.document.getElementById(“elementId”).value = “New Value”;

```

### **10. Enlist and explain any 4 Form elements:**

1. **input**: Used for text input fields, password fields, checkboxes, radio buttons, etc.

2. **textarea**: Multi-line text input field.

3. **select**: Drop-down list for selecting one or more options.

4. **button**: Used to create clickable buttons that trigger actions or submit forms.

### **11. Define Cookies:**


Cookies are small pieces of data stored by a browser on the user’s device. They are used to store user-
specific information, such as login details, session data, and preferences, to improve the user
experience.

### **12. State any two properties and methods of location object (Repeated):**

- **Properties:**

1. `location.pathname` – Returns the path of the current URL.

2. `location.protocol` – Returns the protocol (http or https) of the URL.

- **Methods:**

1. `location.replace()` – Replaces the current document with a new one without storing the history.

2. `location.reload()` – Reloads the current page.

### **13. Write a program to open multiple windows:**

```js

Let win1 = window.open(http://www.example1.com);

Let win2 = window.open(http://www.example2.com);

Let win3 = window.open(http://www.example3.com);

```

### **14. Write a JavaScript to design a form to accept values for user ID & password (Repeated):**

Refer to answer 1.

### **15. Differentiate between session cookies and persistent cookies (Repeated):**

Refer to answer 6.
### **16. Write a JavaScript syntax to access elements of another child window (Repeated):**

Refer to answer 9.

### **17. Write a JavaScript program to change the contents of a window (Repeated):**

Refer to answer 8.

### **18. Write the syntax and explain the use of the following methods of JavaScript Timing Event:**

- **setTimeout()**: Executes a function after a specified delay.

```js

setTimeout(function() { alert(“Hello!”); }, 2000); // Runs after 2 seconds

```

- **setInterval()**: Executes a function repeatedly at specified intervals.

```js

setInterval(function() { alert(“This will repeat every 2 seconds!”); }, 2000);

```

### **19. List ways of Protecting your webpage and describe any one of them:**

1. Use HTTPS for secure communication.

2. Implement Content Security Policy (CSP) to prevent cross-site scripting (XSS).

3. Disable right-click and copy-paste functionality.

4. Secure cookie storage using `HttpOnly` and `Secure` flags.


**Description**: **HTTPS** encrypts communication between the server and the browser, ensuring
that sensitive data like passwords is not intercepted by third parties.

### **20. Write a program to open a new window:**

```js

Window.open(http://www.example.com, “_blank”);

```

### **21. What is the meaning of windows Focus and windows Blur:**

- **Window Focus**: When a window becomes the active window (i.e., user is interacting with it).

- **Window Blur**: When the window loses focus, meaning the user switches to another window or
tab.

### **22. Describe browser location object (Repeated):**

Refer to answer 4.

Let me know if you need any clarifications or further explanations!

Here’s a simple HTML webpage with a form for entering a username and password. The form data
(username and password) is stored as cookies using a JavaScript function:

### HTML + JavaScript Code:

```html

<!DOCTYPE html>

<html>

<head>
<title>Login Form with Cookies</title>

</head>

<body>

<h2>Login Form</h2>

<form onsubmit="storeCookies()">

Username: <input type="text" id="username" required><br><br>

Password: <input type="password" id="password" required><br><br>

<input type="submit" value="Submit">

</form>

<script>

// Function to store the username and password as cookies

function storeCookies() {

// Get username and password from the form inputs

var username = document.getElementById('username').value;

var password = document.getElementById('password').value;

// Set cookies for username and password (with a 7-day expiration)

document.cookie = "username=" + username + "; path=/; max-age=" + 7*24*60*60;

document.cookie = "password=" + password + "; path=/; max-age=" + 7*24*60*60;

alert("Cookies have been set!");

</script>

</body>

</html>

```
### Explanation:

- The form contains two input fields: one for the **username** and another for the **password**.

- When the user submits the form, the JavaScript function `storeCookies()` is triggered.

- The function stores the username and password as cookies with a **7-day expiration** (`max-age` is
set to 7 days).

- The cookies are stored using the `document.cookie` property.

You can expand the code by adding validation or encrypting the password for added security if needed.

Let’s answer all the questions based on the image you’ve uploaded.

### **2. HTML Script for Addition, Subtraction, and Multiplication**

Here is the HTML and JavaScript code for the calculator that performs addition, subtraction, and
multiplication based on user input.

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>Simple Calculator</title>

<script>

Function calculate(operation) {

Let num1 = parseFloat(document.getElementById(“num1”).value);

Let num2 = parseFloat(document.getElementById(“num2”).value);

Let result;
If (operation === ‘add’) {

Result = num1 + num2;

} else if (operation === ‘sub’) {

Result = num1 – num2;

} else if (operation === ‘mul’) {

Result = num1 * num2;

Alert(“Result: “ + result);

</script>

</head>

<body>

<h2>Simple Calculator</h2>

<form>

First Number: <input type=”number” id=”num1”><br><br>

Second Number: <input type=”number” id=”num2”><br><br>

<input type=”button” value=”Add” onclick=”calculate(‘add’)”>

<input type=”button” value=”Sub” onclick=”calculate(‘sub’)”>

<input type=”button” value=”Mul” onclick=”calculate(‘mul’)”>

</form>

</body>

</html>

```

### **3. JavaScript for Creating an Email Address**


This script will accept the user’s first name and organization domain, then form an email address and
display it in the browser window.

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>Email Generator</title>

<script>

Function createEmail() {

Let firstName = document.getElementById(“firstName”).value;

Let domain = document.getElementById(“domain”).value;

Let email = firstName + “@” + domain;

Alert(“Generated Email: “ + email);

</script>

</head>

<body>

<h2>Email Generator</h2>

<form>

First Name: <input type=”text” id=”firstName”><br><br>

Domain: <input type=”text” id=”domain”><br><br>

<input type=”button” value=”Generate Email” onclick=”createEmail()”>

</form>

</body>

</html>
```

### **4. What is a cookie?**

**Cookie**: A cookie is a small piece of data stored on the user’s device by the web browser. Cookies
are used to remember information about the user, such as login details or preferences.

**Need**: Cookies are essential for maintaining sessions, user preferences, and tracking user activities
across web pages.

**Characteristics of Persistent Cookies**:

- Stored on the user’s device until they expire or are deleted.

- Used for long-term tracking and remembering user preferences across sessions.

### **5. Properties of Window Object**

1. **window.innerWidth**: Returns the width of the window’s content area.

2. **window.innerHeight**: Returns the height of the window’s content area.

3. **window.location**: Returns the URL of the current page.

4. **window.history**: Provides access to the browser’s session history.

**JavaScript to Open Popup Windows**:

```html

<script>

// Opens a popup when the page loads

Window.onload = function() {

Window.open(“”, “Welcome”, “width=300,height=200”).document.write(“WELCOME To


SCRIPTING”);

}
// Opens another popup when the page unloads

Window.onunload = function() {

Window.open(“”, “Goodbye”, “width=300,height=200”).document.write(“FUN WITH SCRIPTING”);

</script>

```

### **6. HTML Script to Display Laptop Brands and Image on Hover**

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>Laptop Brands</title>

<style>

.image-box {

Width: 200px;

Height: 150px;

Border: 1px solid #000;

Text-align: center;

Line-height: 150px;

</style>

<script>

Function showImage(brand) {

Let imageBox = document.getElementById(“imageBox”);


If (brand === ‘lenovo’) {

imageBox.innerHTML = “<img src=’lenovo.jpg’ alt=’Lenovo Laptop’ width=’200’ height=’150’>”;

} else if (brand === ‘hp’) {

imageBox.innerHTML = “<img src=’hp.jpg’ alt=’HP Laptop’ width=’200’ height=’150’>”;

} else if (brand === ‘dell’) {

imageBox.innerHTML = “<img src=’dell.jpg’ alt=’Dell Laptop’ width=’200’ height=’150’>”;

} else {

imageBox.innerHTML = “Image”;

</script>

</head>

<body>

<h2>Select a Laptop Brand</h2>

<ul>

<li onmouseover=”showImage(‘lenovo’)”>Lenovo</li>

<li onmouseover=”showImage(‘hp’)”>HP</li>

<li onmouseover=”showImage(‘dell’)”>Dell</li>

</ul>

<div class=”image-box” id=”imageBox”>Image</div>

</body>

</html>

```

This script will display the respective laptop image when you hover over the brand names in the list.
Make sure you have images named `lenovo.jpg`, `hp.jpg`, and `dell.jpg` for the hover effect to work.

### **7. Ways to Protect Your Webpage and Description**


There are several ways to protect your webpage:

1. **HTTPS (SSL/TLS)**: Ensures encrypted communication between the user and the server.

2. **Content Security Policy (CSP)**: Helps prevent cross-site scripting (XSS) and data injection attacks.

3. **Input Validation and Sanitization**: Ensures that user inputs are properly validated to prevent SQL
injection and other attacks.

4. **Disable Right-Click**: Prevents copying content by disabling right-click functionality.

5. **Obfuscate JavaScript Code**: Makes it harder for users to read or manipulate the website’s
JavaScript code.

6. **Password Protection**: Restricts access to certain pages using password protection.

**Example of HTTPS**:

SSL/TLS encrypts data transmitted between the client and server, providing security against
eavesdropping and man-in-the-middle attacks. It authenticates the server to the client and ensures the
integrity of the content.

### **8. Slideshow with Image Transitions (Next and Previous)**

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>Image Slideshow</title>

<script>

Let currentImageIndex = 0;

Const images = [‘image1.jpg’, ‘image2.jpg’, ‘image3.jpg’];


Function showImage(index) {

Const imgElement = document.getElementById(“slideshow”);

imgElement.src = images[index];

Function nextImage() {

currentImageIndex = (currentImageIndex + 1) % images.length;

showImage(currentImageIndex);

Function prevImage() {

currentImageIndex = (currentImageIndex – 1 + images.length) % images.length;

showImage(currentImageIndex);

Window.onload = function() {

showImage(currentImageIndex);

};

</script>

</head>

<body>

<h2>Image Slideshow</h2>

<img id=”slideshow” width=”400” height=”300”>

<br>

<button onclick=”prevImage()”>Previous</button>

<button onclick=”nextImage()”>Next</button>

</body>

</html>

```
### **9. Create and Read Persistent Cookies in JavaScript**

**Creating a Persistent Cookie:**

```javascript

Function setCookie(name, value, days) {

Let expires = “”;

If (days) {

Const date = new Date();

Date.setTime(date.getTime() + (days*24*60*60*1000));

Expires = “; expires=” + date.toUTCString();

Document.cookie = name + “=” + (value || “”) + expires + “; path=/”;

```

**Reading a Cookie:**

```javascript

Function getCookie(name) {

Const nameEQ = name + “=”;

Const ca = document.cookie.split(‘;’);

For(let i=0;I < ca.length;i++) {

Let c = ca[i];

While (c.charAt(0)==’ ‘) c = c.substring(1,c.length);

If (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

}
Return null;

```

### **10. Generate a College Admission Form using HTML**

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>College Admission Form</title>

</head>

<body>

<h2>College Admission Form</h2>

<form action=”/submit” method=”post”>

Name: <input type=”text” name=”name” required><br><br>

Age: <input type=”number” name=”age” required><br><br>

Email: <input type=”email” name=”email” required><br><br>

Phone: <input type=”tel” name=”phone” required><br><br>

Course:

<select name=”course” required>

<option value=”BSc”>BSc</option>

<option value=”BA”>BA</option>

<option value=”BCom”>BCom</option>

</select><br><br>

<input type=”submit” value=”Submit”>


</form>

</body>

</html>

```

### **11. JavaScript Program to Create, Read, Update, and Delete Cookies**

```javascript

// Create a cookie

Function setCookie(name, value, days) {

Let expires = “”;

If (days) {

Const date = new Date();

Date.setTime(date.getTime() + (days*24*60*60*1000));

Expires = “; expires=” + date.toUTCString();

Document.cookie = name + “=” + (value || “”) + expires + “; path=/”;

// Read a cookie

Function getCookie(name) {

Const nameEQ = name + “=”;

Const ca = document.cookie.split(‘;’);

For(let i=0;I < ca.length;i++) {

Let c = ca[i];

While (c.charAt(0)==’ ‘) c = c.substring(1,c.length);

If (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

}
Return null;

// Update a cookie (by simply setting it again)

setCookie(‘username’, ‘newUsername’, 7);

// Delete a cookie

Function deleteCookie(name) {

Document.cookie = name+’=; Max-Age=-99999999;’;

```

### **12. Properties of the Form Tag**

1. **action**: The URL to which the form data is submitted.

2. **method**: The HTTP method (GET or POST) used to submit the form.

3. **enctype**: Specifies how the form data should be encoded when submitted.

4. **target**: Specifies where to display the response after submitting the form (e.g., `_blank` for a new
tab).

### **13. Difference Between GET and POST Methods**

- **GET**:

- Appends form data in the URL.

- Has size limitations.

- Not suitable for sensitive data (like passwords).


**Example**:

```html

<form action=”/submit” method=”get”>

<input type=”text” name=”username”>

<input type=”submit” value=”Submit”>

</form>

```

- **POST**:

- Submits form data in the body of the request.

- More secure and suitable for larger data.

**Example**:

```html

<form action=”/submit” method=”post”>

<input type=”text” name=”username”>

<input type=”submit” value=”Submit”>

</form>

```

### **14. JavaScript to Open a New Window on Button Click**

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>


<title>Open New Window</title>

<script>

Function openWindow() {

Window.open(https://example.com, “_blank”, “width=600,height=400”);

</script>

</head>

<body>

<button onclick=”openWindow()”>Open New Window</button>

</body>

</html>

```

### **15. JavaScript to Create Persistent Cookies for Item Names**

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>Item Cookie</title>

<script>

Function setItemCookie(itemName) {

Document.cookie = “item=” + itemName + “; path=/; max-age=” + 7*24*60*60;

Alert(“Cookie for item “ + itemName + “ has been set.”);

</script>
</head>

<body>

<h2>Set Item Cookie</h2>

<button onclick=”setItemCookie(‘Item1’)”>Set Cookie for Item1</button>

<button onclick=”setItemCookie(‘Item2’)”>Set Cookie for Item2</button>

</body>

</html>

```

### **16. Store Username and Password in Cookies**

This code stores the username and password as cookies when submitted.

```html

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>Login Form with Cookies</title>

<script>

Function storeCookies() {

Let username = document.getElementById(‘username’).value;

Let password = document.getElementById(‘password’).value;

Document.cookie = “username=” + username + “; path=/; max-age=” + 7*24*60*60;

Document.cookie = “password=” + password + “; path=/; max-age=” + 7*24*60*60;


Alert(“Username and Password cookies set!”);

</script>

</head>

<body>

<h2>Login Form</h2>

<form onsubmit=”storeCookies(); return false;”>

Username: <input type=”text” id=”username”><br><br>

Password

You might also like