0% found this document useful (0 votes)
9 views

Unit Test 2 Ans

Unit test 2 CSS answer
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)
9 views

Unit Test 2 Ans

Unit test 2 CSS answer
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/ 13

**2 MARKS**

1. **Attributes used to set the position of a window:**

The following attributes are used to set the position of a window when using
JavaScript's `window.open()` method:

- `left`: Sets the horizontal position (in pixels) of the new window from the left
edge of the screen.
- `top`: Sets the vertical position (in pixels) of the new window from the top
edge of the screen.

Example:
```javascript
window.open("http://example.com", "_blank",
"width=500,height=500,left=200,top=100");
```

2. **Syntax for opening a window:**

The syntax for opening a new window in JavaScript is as follows:


```javascript
window.open(URL, name, specs, replace);
```
- `URL` is the URL of the page to be loaded in the new window.
- `name` is the name of the new window (or the target window).
- `specs` is a comma-separated list of window features (e.g., width, height, left,
top, etc.).
- `replace` is optional and specifies whether the URL should replace the current
page in the history list.

Example:
```javascript
window.open("https://www.example.com", "_blank", "width=600,height=400");
```

3. **Define Cookies:**

Cookies are small pieces of data that are stored by a web browser on the user's
device. They are used to remember information about the user, such as
preferences, login details, or session information. Cookies are sent between the
client and server with each HTTP request and response.

---

**4 MARKS**

1. **Method of setting expiry date of a cookie:**

The expiry date of a cookie can be set using the `expires` attribute in the cookie
string. The `expires` attribute should be set to a specific date in the UTC format.
Example:
```javascript
document.cookie = "username=John; expires=Thu, 18 Dec 2024 12:00:00 UTC";
```

Alternatively, you can set a cookie to expire after a specific period (e.g., 1 day)
by setting the `max-age` attribute:
```javascript
document.cookie = "username=John; max-age=86400"; // expires in 1 day
(86400 seconds)
```

2. **How to create and delete cookies in JavaScript:**

- **Create a Cookie:**
To create a cookie, you simply set the `document.cookie` property to a string
containing the cookie data, along with optional attributes like `expires`, `path`,
`domain`, and `secure`.

Example:
```javascript
document.cookie = "username=JohnDoe; expires=Thu, 18 Dec 2024 12:00:00
UTC; path=/";
```
- **Delete a Cookie:**
To delete a cookie, set its `expires` attribute to a past date. This will prompt the
browser to remove the cookie.

Example:
```javascript
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC;
path=/";
```

3. **What is the use of `setTimeout()` function? Write a JavaScript to


illustrate it:**

The `setTimeout()` function is used to execute a piece of code after a specified


delay (in milliseconds). It is commonly used to delay the execution of code or
create animations.

Syntax:
```javascript
setTimeout(function, delay);
```
- `function` is the code or function to be executed after the delay.
- `delay` is the time in milliseconds before the function is executed.

Example:
```javascript
setTimeout(function() {
alert("This message is displayed after 3 seconds");
}, 3000); // 3000 milliseconds = 3 seconds
```

4. **Explain the use of `focus()` method for window object:**

The `focus()` method is used to give focus to a window or tab. When a window
has focus, it is brought to the front and becomes active, allowing the user to
interact with it. This is commonly used for pop-up windows or input forms that
need attention.

Example:
```javascript
let myWindow = window.open("https://www.example.com");
myWindow.focus(); // Brings the opened window to the front
**2 MARKS**

1. **Define Frame:**
A **frame** in HTML is a section of a browser window that can display content
independently from the rest of the window. Frames are used to divide the
window into multiple areas where different documents or web pages can be
shown. The `<iframe>` or `<frameset>` tags are commonly used to create frames
in web pages.

Example:
```html
<iframe src="https://www.example.com" width="300" height="200"></iframe>
```

2. **Define Rollover:**
A **rollover** effect occurs when the user moves the mouse pointer over an
element, such as an image or button, causing it to change its appearance.
Common rollover effects include changing the image, background color, or text.

Example:
```html
<img src="image1.jpg" onmouseover="this.src='image2.jpg';"
onmouseout="this.src='image1.jpg';">
3. **Define the term - Regular Expression:**
A **regular expression** (regex) is a sequence of characters that defines a
search pattern. It is used for string pattern matching and manipulation, such as
validating, searching, replacing, and extracting text from strings.

Example of a simple regex pattern:


- `/\d+/` matches one or more digits.

4. **Write a JavaScript to display frames without border:**


You can create an `<iframe>` without a border by setting the `frameborder`
attribute to `0` or using CSS to remove borders.

Example:
```html
<iframe src="https://www.example.com" width="300" height="200"
frameborder="0"></iframe>
```

5. **Give the regular expression for matching digits and non-digits:**

- **Matching digits:** `/\d+/` (matches one or more digits).


- **Matching non-digits:** `/\D+/` (matches one or more non-digits).
Here:
- `\d` matches any digit (0-9).
- `\D` matches any character that is not a digit.
**4 MARKS**

1. **How will you make the rollover more efficient?**

To make the rollover effect more efficient:


- **Use CSS hover effects**: For simple image or style changes on hover, CSS is
much more efficient than JavaScript, as it doesn't require event listeners or DOM
manipulation.

Example:
```css
img:hover {
content: url('image2.jpg');
}

- **Preload images**: If you're changing images during the rollover, preload


images to ensure that there is no delay when the user hovers over the element.
```javascript
var img1 = new Image();
img1.src = "image1.jpg";
var img2 = new Image();
img2.src = "image2.jpg";
- **Limit event listeners**: Instead of adding individual event listeners to each
element, use event delegation or group similar elements together to minimize the
number of listeners.

2. **Explain any four commonly used methods in regular


expression:**

- **`test()`**: Tests whether a pattern exists in a string. Returns `true` if a match


is found, otherwise `false`.
```javascript
let regex = /\d+/;
console.log(regex.test("abc123")); // true
```

- **`exec()`**: Executes a search for a match in a string and returns an array


with the match results. If no match is found, it returns `null`.
```javascript
let regex = /\d+/;
let result = regex.exec("abc123");
console.log(result); // ["123"]
```

- **`match()`**: Used on a string to find matches with a regular expression. It


returns an array of all matches or `null` if no match is found.
```javascript
let str = "abc123";
let result = str.match(/\d+/);
console.log(result); // ["123"]

- **`replace()`**: Replaces a matched pattern in a string with a new substring.


```javascript
let str = "Hello 123";
let newStr = str.replace(/\d+/, "456");
console.log(newStr); // "Hello 456"
```

3. **Explain `<frameset>` tag along with the attributes used in it:**


The `<frameset>` tag is used to create a set of frames in a webpage, replacing
the `<body>` tag. It allows you to divide the browser window into multiple
regions, each capable of displaying a different document.

**Attributes of `<frameset>`:**
- **`cols`**: Specifies the number and size of the columns in the frameset (e.g.,
`cols="50%,50%"`).
- **`rows`**: Specifies the number and size of the rows in the frameset (e.g.,
`rows="30%,70%"`).
- **`border`**: Specifies the width of the border between frames.
- **`frameborder`**: Specifies whether the frames should have a border
(`frameborder="0"` means no border).
- **`bordercolor`**: Sets the color of the border between frames.

Example:
```html
<frameset cols="25%,75%">
<frame src="sidebar.html" />
<frame src="content.html" />
</frameset>
```

4. **Write a JavaScript in which when the user rolls over the name of
the fruit:**

The following JavaScript can be used to change the image when the user hovers
over the name of a fruit:

```html
<html>
<body>
<p onmouseover="changeImage('apple.jpg')"
onmouseout="resetImage()">Apple</p>
<img id="fruitImage" src="apple.jpg" width="100" height="100" />

<script>
function changeImage(imageSrc) {
document.getElementById("fruitImage").src = imageSrc;
}
function resetImage() {
document.getElementById("fruitImage").src = "default.jpg";
}
</script>
</body>
</html>
In this example, when the user hovers over the word "Apple," the image
changes to "apple.jpg" and resets back to "default.jpg" when the mouse moves
out.

5. **Write a JavaScript to change the contents of one frame from


another frame:**

To change the content of one frame from another, you can use JavaScript to
access the `window.frames` object and modify the `src` attribute of the target
frame.

Example:
```html
<frameset rows="50%,50%">
<frame src="frame1.html" name="frame1" />
<frame src="frame2.html" name="frame2" />
</frameset>
<script>
// Change the content of frame2 from another script
window.frames['frame2'].location.href = 'newpage.html';
</script>
In this example, the content of `frame2` is changed to "newpage.html" using JavaScript from the
parent window.

You might also like