Wednesday, April 23, 2025

JavaScript Redirect to Another Site

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

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...