Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the browser to navigate to URL in JavaScript [duplicate]

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?

like image 409
jjujuma Avatar asked Aug 04 '09 10:08

jjujuma


People also ask

How do I navigate to another URL?

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.

How do I get the URL of a website using JavaScript?

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.

How do I change URL without reloading?

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.

How do I link one JavaScript page to another?

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") .


2 Answers

Try these:

  1. window.location.href = 'http://www.google.com';
  2. window.location.assign("http://www.w3schools.com");
  3. window.location = 'http://www.google.com';

For more see this link: other ways to reload the page with JavaScript

like image 37
tilak Avatar answered Nov 10 '22 07:11

tilak


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('...'); 
like image 115
Paolo Bergantino Avatar answered Nov 10 '22 05:11

Paolo Bergantino