How You Can Redirect to Another Web Url Using JavaScriptor JQuery?

How You Can Redirect to Another Web Url Using JavaScriptor JQuery?
----------------------------------------------

To redirect page you can use javascript which is quite usefull to make this work window.location property to make a page redirect for this there is no need for jQuery.
If you do console.log(window.location)  you can see list of object return by browser.

console.log(window.location)

//Retun by browser
Location {href: "https://coderuck.com/", ancestorOrigins: DOMStringList, origin: "https://coderuck.com", protocol: "https:", host: "coderuck.com", …}
ancestorOrigins: DOMStringList {length: 0}
origin: "https://coderuck.com"
protocol: "https:"
host: "coderuck.com"
hostname: "coderuck.com"
port: ""
pathname: "/"
search: ""
hash: ""
href: "https://coderuck.com/"
assign: ƒ assign()
reload: ƒ reload()
toString: ƒ toString()
replace: ƒ replace()
valueOf: ƒ valueOf()
Symbol(Symbol.toPrimitive): undefined
__proto__: Location

As you see if return serveral properties which you can use.But for redirect we will use window.location.replace(...) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button process.

If you want to redirect by clicking/button on the link then use location.href

If you want to redirect HTTP auto then use location.href

Example

For example:

// redirect auto HTTP
window.location.replace("https://coderuck.com");

// redirect by clicking on the link/button
window.location.href = "https://coderuck.com";

 

Categories: Java Script Tags: #JavaScript,

Newsletter Subcribe

Receive updates and latest news direct from our team. Simply enter your email.