Javascript redirect new url
A simple but useful task!
In JavaScript, you can redirect to a new URL using the window.location
object or the document.location
object. Here are a few ways to do it:
Method 1: Using window.location
window.location.href = 'https://www.example.com';
This will redirect the user to the specified URL.
Method 2: Using document.location
document.location.href = 'https://www.example.com';
This is similar to the first method, but uses the document
object instead of window
.
Method 3: Using window.location.replace
window.location.replace('https://www.example.com');
This method is similar to the first two, but it replaces the current page with the new URL, rather than adding it to the browser's history.
Method 4: Using window.location.assign
window.location.assign('https://www.example.com');
This method is similar to the first two, but it assigns the new URL to the current page, rather than replacing it.
Method 5: Using a timeout
setTimeout(function() {
window.location.href = 'https://www.example.com';
}, 0);
This method uses a timeout to redirect the user to the new URL after a short delay (in this case, 0 milliseconds).
Method 6: Using a function
function redirect(url) {
window.location.href = url;
}
redirect('https://www.example.com');
This method defines a function that takes a URL as an argument and redirects the user to that URL.
Remember to replace https://www.example.com
with the actual URL you want to redirect to!