Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the URL address from the browser in ReactJS (servers URL)

I can parse are HTTP response of one of the intial calls and save that in the state but there must be another way to get the URL?

I am using the npm 'rest' library and want to specify a different port but I do not know the path on which machine the server will be installed so I need the server's url.

Solution

window.location.hostname

like image 590
ALM Avatar asked Jun 14 '16 23:06

ALM


People also ask

How do I get the URL in React JS?

Use the window object to get the current URL in React, e.g. window. location. href returns a string containing the whole URL. If you need to access the path, use window.

How do you get the path name in React?

Using location props location. pathname can get you the current URL pathname inside a React component.

How do I show image URL in React?

To display an image from a URL, use the img tag and set its src prop to the complete URL of the image. Optionally set the alt prop to a short description of the image.


1 Answers

location is a property on the window object with a bunch of information, you can get the url with window.location.href

You can get the hostname with window.location.hostname

like image 155
omarjmh Avatar answered Oct 18 '22 01:10

omarjmh