0

I am working on ReactJs and the server direct to a domain name with id on it. So I want to ask how can I get current domain name with ReactJS?

1

3 Answers 3

2

This isn't React specific.

Just look at window.location:

alert(window.location.hostname)

(Yes, the above snippet will say stacksnippets.net due to security reasons.)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I got this error: ReferenceError: window is not defined. It is jsdom jsdom-global library right?
window is innately available in browsers, you don't need a library. Are you running this in JSDom?
I found the answer. I use context.req.url of getInitialProps ReactJS, so I dont have to use window.
There is no "context.req.url" in raw ReactJS, so you left essential detail out of your original question. Glad you found the answer though.
1

Use window.location inside componentDidMount React Lifecycle. If you want to use inside DOM use state to store value.

1 Comment

I found the answer. I use context.req.url of getInitialProps ReactJS, so I dont have to use window.
1

Use window.location.hostname - note this will not work in JSDom, only in browsers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.