Redirect pages using JavaScript and jQuery
- In JavaScript, window.location property is being used to make a page redirect, where don't need any jQuery for this.
- The location.assign() method reasons the window to load and display the document at the URL specified.
Syntax of Location.assign() method:
location.assign(URL);
- In JavaScript, URL is an acronym for Uniform Resource Locator.
- And it is a reference (an address) to a resource on the Internet.
Sample Code :
<!DOCTYPE html>
<html>
<head>
<title>Wikitechy - redirect page using JavaScript </title>
<script>
function myFunction()
{
location.assign("http://www.wikitechy.com");
}
</script>
</head>
<body>
<button onclick="myFunction()">
click here
</button>
</body>
</html>
Code Explanation :
In JavaScript the function myFunction() specifies the starting function. The location.assign() method causes the window to load and display the document at the “http://www.wikitechy.com” URL specified. Here the <button onclick="myFunction()"> specifies the onclick event that occurs when the user clicks on an element it call the myfunction() name , thereby the website www.wikitechy.com opens in the new tab.
Sample Output :
After Clicking the button “Click here”, it displays the “http://www.wikitechy.com” wikitechy website as shown below: