JavaScript and Web Development Theory Questions
1) Explain open() method of window object with syntax.
The open() method is used to open a new browser window or tab. Syntax: `window.open(URL,
name, specs, replace)`. The URL specifies the page to open, name sets the window's target, specs
define window features like height, width, etc., and replace specifies if the URL will replace the
current entry in the history list.
2) State and explain what is a session cookie?
A session cookie is a type of cookie that is stored temporarily in a browser's memory while the user
is browsing. Once the user closes the browser, the session cookie is deleted. It's used for keeping
track of user information within the session, like login status.
3) Define the use of g, m, and i flags in regex with example.
`g` flag stands for global search, `m` flag is for multiline search, and `i` flag is for case-insensitive
search. Example: `/hello/gi` will match all instances of 'hello' in any case within a text.
4) Describe Quantifiers with the help of example.
Quantifiers in regex are symbols that define the number of occurrences. `*` matches zero or more,
`+` matches one or more, and `{n,m}` matches between n and m times. Example: `/a{2,3}/` matches
'aa' or 'aaa'.
5) Write a JavaScript syntax to access elements of another child window.
To access elements of another child window, use:
`childWindow.document.getElementById('elementID')`.
6) Define a floating menu with example.
A floating menu stays visible on the screen as the user scrolls. Example: CSS with `position: fixed;`
keeps the menu fixed in place.
7) Give syntax and explain the use of setTimeout() function with example.
`setTimeout()` executes a function after a specified delay. Syntax: `setTimeout(function,
milliseconds)`. Example: `setTimeout(() => alert('Hello'), 2000);` shows an alert after 2 seconds.
8) Enlist any four mouse events with their use.
Four mouse events: `click` - triggered when an element is clicked, `mouseover` - triggered when
mouse hovers over, `mouseout` - triggered when mouse leaves, `dblclick` - triggered on
double-clicking an element.
9) Write a JavaScript program to change contents of a window.
```javascript
window.document.body.innerHTML = 'New Content';
``` Changes the content of the current window.
10) Explain test() and exec() methods of Regular Expression object.
`test()` checks if a pattern exists, returning true/false. `exec()` returns matched content if found or
null if not. Example: `/a/.test('abc')` is true; `/a/.exec('abc')` returns ['a'].
11) Explain validating menu selection with example.
Menu validation ensures the user selects a valid option. Example: use `if (dropdown.value ===
'validOption')` to confirm selection.
12) Create a slideshow with next and previous buttons in JavaScript.
Use an array to store image URLs and `setInterval()` to display them with next/previous buttons.
13) JavaScript to search for 'MSBTE' in a string.
```javascript
let text = 'string';
if (/MSBTE/.test(text)) {
console.log('Pattern is found');
} else {
console.log('Pattern is not found');
```
14) JavaScript to store password as a cookie on change.
```javascript
function storeCookie() {
document.cookie = 'password=' + document.getElementById('password').value;
```
15) JavaScript for a crawling status bar message.
Use `setInterval()` to display the message scrolling left to right.
16) JavaScript to create rotating banner ads.
Rotate banners using `setInterval()` and link ads to URLs.
17) Web page with aadhaar card validation.
```javascript
if (/\d{12}/.test(aadhaarInput.value)) {
console.log('Valid');
} else {
console.log('Invalid');
```
18) JavaScript program for banner ads with URLs.
Link banners using `window.location.href` based on selection.
19) JavaScript for name and email validation on submit.
Use regex: `/^[a-zA-Z]+$/` for names and `/^\S+@\S+\.\S+$/` for emails.
20) Describe navigator object and display browser info.
`navigator` object provides browser info. `navigator.appName` and `navigator.appVersion` display
name and version.
21) Explain search() method in regex with example.
`search()` finds first match position. Example: `'hello world'.search(/world/)` returns 6.
22) Text rollover example.
Using `onmouseover` and `onmouseout`, change an element's appearance on hover.
23) JavaScript pull-down menu with redirect.
On selecting an option, use `window.location.href` to navigate.
24) JavaScript for read, update, and delete cookies.
Use `document.cookie` to create and manage cookies.
25) What is regular expression with example.
Regex is a pattern for text matching. Example: `/\d/` matches any digit.
26) JavaScript to open a new window with two frames.
Use `window.open()` for a new window, with frames using `frameset`.
27) JavaScript to display images in a new window based on selection.
On selecting, open a new window with `window.open(imageURL)`.