Implement popup and redirect in JavaScript
BitkernelSoftware Engineer · Online AssessmentLast updated
This question evaluates proficiency with client-side JavaScript, including browser APIs, DOM-based UI creation for popups, event handling, navigation mechanisms, and simple input validation for redirects.
You are working in a browser environment and must use vanilla JavaScript only (no external libraries).
Task
Implement a function showPopupAndRedirect(message, targetUrl) that does the following:
- Immediately shows a popup dialog to the user displaying the given
message. - After the user closes the popup, the current page should redirect to
targetUrl.
Requirements
- The popup must be created using only built-in browser APIs (for example,
alert,confirm, or a custom DOM-based modal that you implement yourself). - The redirect must be done using standard browser navigation (e.g., assigning to
window.locationor a similar mechanism). - If
targetUrlis not a non-empty string, the function should not attempt to redirect. - Assume this code runs in a modern browser with JavaScript enabled.
You do not need to handle errors beyond the simple validation of targetUrl described above.
Describe the implementation and then provide the JavaScript code for showPopupAndRedirect.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.