Redirection means that when an HTTP request for one page automatically goes to another page. Usually redirection of a web page is used for redirecting to a different domain for example when we move our weebsite from one URL to another URL or version change of a web page.
There are two types of redirection that are client side and server side. JQuery and JavaScript are mostly used for client-side redirection.
Here we are discuss about how to redirect weeb page using jquery / Javascript.
We can use the JavaScript 'window.location' property for page redirection.
Option 1: Web Page Redirection using replace() function:
replace() function of window.location object removes the current web page(URL) from the history and replaces it with the new web page (URL). In this method user unable to use the browser back button to navigate previous page.
Syntax:
window.location.replace("new web page_url")
Example:
window.location.replace("https://www.thelinuxfaq.com/")
Option 2: Using window.location.href:
If we want to redirect the URL when the user click on the button elements, we will use 'window.location.href' for redirection.
Syntax:
window.location.href("new web page_url").
Example:
window.location.href("https://www.thelinuxfaq.com/")
Option 3: Using window assign() function:
window.location.assign() function also used to redirect the weeb page. This adds the new URL in the history to redirect the web page using javascript. Comparing to replace function, it allows the user to get back to the original document using browser back button element.
Syntax:
window.location.assign("new web page_url").
Example:
window.location.assign("https://www.thelinuxfaq.com/")
Option 4: Using navigate function:
navigate function is the same as window.location="url" but it loads the web page from the cache rather directly request from the server.
Syntax:
window.navigate("page.html").
window.location.assign("https://www.thelinuxfaq.com/")
Example:
window.navigate("https://www.thelinuxfaq.com/")
Option 5: Redirect web page using JQuery
All the above are javascript commands. Now we are going to learn how to redirect web page using JQuery. JQuery API also allows the user to redirect the web page using attr() function. href command loads the url from the cache but attr() directly request from the server.
var url = "https://www.thelinuxfaq.com/";
$(location).attr('href',url);
Comments (0)