There are several ways to create forced redirects using JavaScript. Here is a simple example:
// Redirect to a new page after 5 seconds
setTimeout(function() {
window.location.href = "https://yournewpage.com";
}, 5000);
// Display a notification before the redirect
alert("You are being redirected to a new page!");
// Alternatively, you can use a modal or a pop-up to display the notification
This code uses the setTimeout()
function to delay the redirect by 5 seconds. Before the redirect, it displays an alert with a message that notifies the user of the redirection.
To make the notification more noticeable, you can use a modal or a pop-up instead of an alert. For example:
// Display a modal before the redirect
var modal = document.createElement("div");
modal.style.position = "fixed";
modal.style.top = 0;
modal.style.bottom = 0;
modal.style.left = 0;
modal.style.right = 0;
modal.style.background = "rgba(0, 0, 0, 0.5)";
modal.innerHTML = "<p>You are being redirected to a new page!</p>";
document.body.appendChild(modal);
// Redirect to a new page after 5 seconds
setTimeout(function() {
window.location.href = "https://yournewpage.com";
}, 5000);
This code creates a modal with a semi-transparent background and a message that notifies the user of the redirection. It then appends the modal to the document body. After 5 seconds, it redirects the user to the new page.
Asked: 2022-02-26 11:00:00 +0000
Seen: 20 times
Last updated: Feb 23 '23