Jquery Redirect Window Location Href

When an HTTPhường request for one page automatically goes khổng lồ another page then it is called a redirection. The redirection of a website page is used for different reasons e.g. redirecting to lớn a different tên miền e.g. when you move your trang web from one URL khổng lồ another URL or redirecting lớn a more recent version of a page you are looking at it. There are many ways lớn redirect a web page & different website technologies provide different ways e.g. Java provides sendRedirect() and forward() method for redirection. The redirection can also be divided into lớn two categories, client side và hệ thống side. In client side, redirection client is responsible for routing request to another URL but in server side redirection, its server"s job to lớn redirect to lớn a new page. The jQuery & JavaScript are mostly used for client side redirection và there are a number of ways to redirect a web page using JavaScript In this tutorial, we"ll examine some of the most popular and comtháng ways to redirect a web page using JavaScript and jQuery.

How lớn redirect a web page using JavaScript

Before looking at the jQuery code, let"s first learn how to lớn redirect a web page by using plain JavaScript. It provides several simple ways to achieve sầu this task without loading an external JavaScript library lượt thích jQuery.

Redirection using window.location.href of Javascript

The most common way lớn redirect an URL using JavaScript is just setting the href attribute of the window.location object. This simulates normal navigation to lớn a new page. I don"t think any task can be simpler than this, just assign a new URL. For example, if you are redirecting URL A khổng lồ URL B then just put the URL B here as shown in the following example: window .location .href = "http://newURL.com" ; Tip: You should remember that window.location.href loads page from browser"s cache and does not always sover the request to lớn the hệ thống. So, if you have an old version of the page available in the cađậy then it will redirect khổng lồ there instead of loading a fresh page from the VPS.

Redirection using replace() function

Another way to redirect lớn a website page in jQuery is by using the replace() function of the window.location object. This method removes current URL from the history và replaces it with a new URL. Here is an example of how to use this function for redirection: window .location .replace ("url" ); Tip: Use the replace() method if you want lớn want to lớn redirect lớn a new page and don"t allow the user to navigate khổng lồ the original page using the baông xã button.

Redirection using window assign() function

You can also use the window.location.assign() function to redirect a website page using JavaScript. This function adds new URL to the history staông chồng & redirected to the new URL, which means, unlượt thích replace() function you can use the back button to lớn go back khổng lồ the original document. Here is how you can use this method khổng lồ redirect to lớn a new đường dẫn window .location .assign("url" ); Tip: Use the assign() method for redirection if you want to lớn allow the user to lớn use the baông chồng button to lớn go baông xã lớn the original document.

Redirection using navigate function

This function is same as window.location="url" & it also loads the page from the cađậy instead of directly requesting it from the server. Here is an example khổng lồ use the navigate() function for redirection to lớn a new page window .navigate ("page.html" ); This code is similar khổng lồ the first method & the page will be loaded from the cache if available instead of sending a new request lớn the VPS.

How lớn redirect website page using jQuery

The jQuery API also allows you lớn redirect a website page lớn another URL in just one line of code. Though this is not the ikhuyến mãi way of using jQuery and but since we all use jQuery for different needs in a website project, it makes sense to leverage it for whatever feature we can. You can use the attr() function of jQuery khổng lồ redirect a website page as shown below: var url = "http://google.com" ; $ (location).attr("href" ,url); There is a slight difference in using attr() vs window.location.href of JavaScript. The $(location).attr("href", url) will request a fresh page from server, while window.location.href loads pages from cađậy. Here is a complete website page which redirects khổng lồ another URL using jQuery: How khổng lồ redirect a webpage using jQuery

Redirecting khổng lồ another URL ..

and here is the output of this web page when you run it on a website browser e.g. internet explorer, chrome or edge:

Bạn đang xem: Jquery redirect window location href

You can see that page is redirecting, once the redirect will be complete, the URL và page title will also be replaced by the target URL.  See Head First jQuery to lớn learn more about jQuery & its powerful API.

How to lớn redirect lớn the previous page

Another case of redirection is going lớn the previous page, here are some of the comtháng way khổng lồ go khổng lồ the previous page using JavaScript"s window object: window .history .back (); // Simulates a baông chồng button cliông chồng window .history .go (- 1 ); // Simulates a back button cliông chồng window .history .bachồng (- 1 ); // Simulates a baông chồng button cliông xã All three methods simulate a back button click programmatically. You can use any of the methods you want to lớn use. That"s all about how to redirect a website request khổng lồ another website page using JavaScript and jQuery. As you have sầu seen there are multiple ways to redirect but there is subtle differences between them e.g. $(location).attr("href", url) gọi of jQuery will open a fresh page from the hệ thống, but the Javascript window.location.href loads pages from cabịt. Similarly, the window.location.assign(url) adds new URL to lớn the document history and redirected lớn the new URL but window.location.replace("URL") removes current URL from history and replaces it with a new URL. Further Reading The Complete jQuery Course: From Beginner To Advanced! Up và Running with jQuery jQuery Fundamentals By Dan Wahlin Other jQuery tutorials you may lượt thích to explore

Xem thêm: Tiêu Chuẩn Nsf Là Gì ? Tại Sao Nên Dùng Sản Phẩm Có Chứng Nhận Nsf?

5 Books to lớn learn jQuery for Web developers (books) How to lớn get current URL, Parameters, và Hash using jQuery? (solution) How to lớn modify more than one element in one line? (example) jQuery Hello World Example (program) Free jQuery eBooks and PDFs (download) How to use more than one jQuery UI DatePicker in JSP page? (example) How to lớn create tab-based UI using jQuery? (example) Top đôi mươi jQuery Interview Questions for Java Web Developers (list) How khổng lồ check/unkiểm tra checkboxes in jQuery (solution) How to download jQuery dynamically in a website page? (solution)
Thanks for reading this article. If you lượt thích this article then please giới thiệu with your friends & colleagues. If you have any question or feedback then please drop a comment.