Implement popup and redirect in JavaScript
Task
Implement a JavaScript function showPopupAndRedirect(message, targetUrl) that operates within a browser environment using only vanilla JavaScript (no external libraries).
The function must perform the following steps:
- Immediately display a popup dialog to the user, showing the provided
message. - Once the user closes this popup, the current page should then navigate to the specified
targetUrl.
Requirements
- The popup must be created using only built-in browser APIs, such as
alert,confirm, or a custom DOM-based modal implemented by the candidate. - The page redirect must utilize standard browser navigation mechanisms (e.g., assigning to
window.location). - A redirect should only occur if
targetUrlis a non-empty string. If it is not, the function must not attempt any redirection. - Assume the code will run in a modern browser with JavaScript enabled.
- Error handling is not required beyond the
targetUrlvalidation specified above.
Candidates are expected to describe their implementation approach and provide the corresponding JavaScript code for showPopupAndRedirect.
AI interview prep
Core dilemma
Balancing the simplicity and immediate availability of blocking browser APIs (`alert`, `confirm`) with the flexibility, non-blocking nature, and improved user experience offered by a custom DOM-based modal, all while adhering to the 'vanilla JS' constraint and ensuring correct asynchronous sequencing of popup closure and redirect.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement popup and redirect in JavaScript
Task
Implement a JavaScript function showPopupAndRedirect(message, targetUrl) that operates within a browser environment using only vanilla JavaScript (no external libraries).
The function must perform the following steps:
- Immediately display a popup dialog to the user, showing the provided
message. - Once the user closes this popup, the current page should then navigate to the specified
targetUrl.
Requirements
- The popup must be created using only built-in browser APIs, such as
alert,confirm, or a custom DOM-based modal implemented by the candidate. - The page redirect must utilize standard browser navigation mechanisms (e.g., assigning to
window.location). - A redirect should only occur if
targetUrlis a non-empty string. If it is not, the function must not attempt any redirection. - Assume the code will run in a modern browser with JavaScript enabled.
- Error handling is not required beyond the
targetUrlvalidation specified above.
Candidates are expected to describe their implementation approach and provide the corresponding JavaScript code for showPopupAndRedirect.