What is the best (correct, modern, cross-browser, safe) way to get a web browser to navigate to a URL of your choice using JavaScript?
To navigate to a new URL, use the location object from the Browser's History API. The session history lets you reassign the location object to a new URL or use the href property on that same object. The syntax for this approach is: window.
You can simply call the window. location. href property which will return you the complete URL of the webpage including hostname, pathname, and query string. Let's test the JavaScript property practically.
Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.
Answer: Use the JavaScript window. location Propertylocation property to make a page redirect, you don't need any jQuery for this. If you want to redirect the user from one page to another automatically, you can use the syntax window. location. replace("page_url") .
Try these:
window.location.href = 'http://www.google.com';
window.location.assign("http://www.w3schools.com");
window.location = 'http://www.google.com';
For more see this link: other ways to reload the page with JavaScript
This works in all browsers:
window.location.href = '...';
If you wanted to change the page without it reflecting in the browser back history, you can do:
window.location.replace('...');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With