We can use JavaScript to redirect the user to another site by changing the window.location
property to the URL of the new site:
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
function redirect() {
window.location.assign("https://google.com");
}
</script>
<input type="button" value="To Google"
onclick="redirect();">
</body>
</html>
This script defines a JavaScript function called redirect()
that uses the window.location.assign()
method to redirect the user to the Google homepage.
The window.location.assign()
method navigates the browser to the URL specified in its argument, effectively redirecting the user to a new page.
The script also creates an HTML input
element with a button type and the text "To Google". When the button is clicked, the onclick
attribute calls the redirect()
function, which redirects the user to the Google homepage.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body onload="redirect()">
<script>
function redirect() {
window.location.assign("https://google.com");
}
</script>
</body>
</html>
This code uses the onload
event to automatically redirect the user to the Google homepage when the page loads.
The onload
event fires when the page has finished loading, so when the user opens the page, the redirect()
function is called automatically.
The redirect()
function uses the window.location.assign()
method to navigate the browser to the URL specified in its argument, which in this case is "https://google.com".
No comments:
Post a Comment