Unit Test 2 Ans
Unit Test 2 Ans
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");
```
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**
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)
```
- **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=/";
```
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
```
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:
```html
<iframe src="https://www.example.com" width="300" height="200"
frameborder="0"></iframe>
```
Example:
```css
img:hover {
content: url('image2.jpg');
}
**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.
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.