<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Website Redirect</title>
</head>
<body>
<script>
// List of websites to choose from
const websites = [
"https://www.example.com",
"https://www.example.org",
"https://www.example.net"
];
// Get a random index from the list
const randomIndex = Math.floor(Math.random() * websites.length);
// Redirect the user to the random website
window.location.href = websites[randomIndex];
</script>
</body>
</html>
websites
array to include additional options for redirection. When the page is loaded, the JavaScript code will select a random website from the list and redirect the user to that website.add it inside when button got clickedYou can add more websites to thewebsites
array to include additional options for redirection. When the page is loaded, the JavaScript code will select a random website from the list and redirect the user to that website.
Certainly! If you want the redirection to happen when a button is clicked, you can modify the code to include an event listener on a button click. Here's an example:add it inside when button got clicked
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Website Redirect</title>
</head>
<body>
<button id="redirectButton">Click to Visit a Random Website</button>
<script>
// List of websites to choose from
const websites = [
"https://www.example.com",
"https://www.example.org",
"https://www.example.net"
];
// Function to redirect to a random website
const redirectToRandomWebsite = () => {
const randomIndex = Math.floor(Math.random() * websites.length);
window.location.href = websites[randomIndex];
};
// Event listener for button click
document.getElementById("redirectButton").addEventListener("click", redirectToRandomWebsite);
</script>
</body>
</html>
redirectToRandomWebsite
function is called, which selects a random website from the list and redirects the user to that website.To prevent the same website from appearing more than once in the random selection process, you can keep track of the websites that have already been visited and remove them from the list before selecting the next random website. Here's an updated code snippet that ensures the same website is not visited twice or more:prevent reappearing of website twice or more
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Website Redirect</title>
</head>
<body>
<button id="redirectButton">Click to Visit a Random Website</button>
<script>
// List of websites to choose from
const websites = [
"https://www.example.com",
"https://www.example.org",
"https://www.example.net"
];
// Array to store visited websites
let visitedWebsites = [];
// Function to redirect to a random website
const redirectToRandomWebsite = () => {
// Remove visited websites from the list
const availableWebsites = websites.filter(website => !visitedWebsites.includes(website));
if (availableWebsites.length === 0) {
alert("You have visited all websites!");
return;
}
const randomIndex = Math.floor(Math.random() * availableWebsites.length);
const selectedWebsite = availableWebsites[randomIndex];
// Add the selected website to visited websites
visitedWebsites.push(selectedWebsite);
// Redirect to the selected website
window.location.href = selectedWebsite;
};
// Event listener for button click
document.getElementById("redirectButton").addEventListener("click", redirectToRandomWebsite);
</script>
</body>
</html>
visitedWebsites
array keeps track of the websites that have already been visited. Before selecting a random website, the code filters the websites
array to exclude the visited websites. If all websites have been visited, an alert is shown. Otherwise, a random website from the available websites is selected for redirection, and the selected website is added to the visitedWebsites
array to prevent it from being visited again.