How to redirect to another page using jquery

Redirecting a web page means, taking the user to a new location. Many trang web uses redirects for many different reasons, e.g. Some websites redirect users from the old tên miền to lớn new domain name, some redirect from one page lớn another e.g. a more relevant page. If you are a Java programmer và worked previously with Servlet & JSP, then you might be familiar with SendRedirect & Forward methods, which are used lớn redirect users in web applications. Actually, there are two kinds of redirect, Server side redirect, and client-side redirect. In Server side redirect, server initiates redirection, while in a client-side redirect, client code does the redirection. Redirecting a website page using JavaScript và JQuery is a client-side redirection.

Bạn đang xem: How to redirect to another page using jquery

Well, HTTPhường. redirection is a big topic & different people do it differently. e.g. bloggers mostly used platforms like WordPress, Blogger feature to lớn redirect a page, though this might be restricted khổng lồ the same tên miền.In this JavaScript & jQuery tutorial, we will learn a new JQuery tip to lớn redirect a page.Btw, If you are learning jQuery then I also suggest you keep a copy of Head First jQuery, I have sầu and always look back on this book whenever I stuông xã. It has got some good examples, which you can always refer baông xã to lớn.And, If you want khổng lồ learn Modern JavaScript or màn chơi up your skills then I suggest you joinThe Complete JavaScript Course: Build Real Projectby Jonas Scmedtmann on Udemy. It"s one of the best & hand-son course to learn ES 6 và other new Javascript features.

JQuery Code lớn redirect a page or URL.

Here is the JQuery code for redirecting a page. Since I have sầu put this code on $(document).ready() function, it will exexinh tươi as soon as the page is loaded.

Xem thêm: Top 4 Cung Hoàng Đạo Giả Tạo Nhất Trên Đời, 12 Cung Hoàng Đạo

You can even pass URL directly to attr() method, instead of using a variable.

JavaScript Code for redirecting a URL

It"s even simpler in JavaScript. You only need lớn change window.location.href property to redirect a page. Though some people also use window.location only, which is also fine. If you are curious about the difference between window.location & window.location.href, then you can see that later one is setting href property explicitly, while earlier one does it implicitly. Since window.location returns an object, which by mặc định sets it"s .href property.
There is another way khổng lồ redirect a page using JavaScript, replace() method of window.location object. You can pass a new URL to lớn replace() method and it will simulate an HTTP.. redirect.By the way, rethành viên that window.location.replace() method doesn"t put the originating page in the session history, which may affect the behaviour of the baông chồng button. Sometimes, it"s what you want, so use it carefully.

HTML Example of Redirecting a Page or URL

Here is a complete HTML page, which uses the above method khổng lồ redirect a page or URL. Since you can only use one method at a time, I have commented rest of the code for redirection. You can unphản hồi và try them as well.