Pavel Poberezhnyi
Pavel Poberezhnyi

Reputation: 773

React.js - how to navigate to other domain?

I'm working on editor where user can set link to another website , for example stackoverflow.com. If user set this url to <a> element and then click on this link he will be navigated to http://myapp.com/stackoverflow.com but not to http://stackoverflow.com. I supposed that this is caused by changed default behavior of element. How can i make this link external? if user will copy url from browser and paste he will paste http://stackoverflow.com and hence everything will works fine. But in case he manually enter stackoverflow.com nothing will work fine. P.s. i also would like to make each element with attribute target="_blank" so, i assume that i can't navigate programatically Any ideas?

Upvotes: 2

Views: 1042

Answers (1)

m1kael
m1kael

Reputation: 2851

Try setting the <a>'s href attribute to the full url, not just stackoverflow.com:

<a href="http://stackoverflow.com" />

Otherwise it's treated as a relative url.

Add a check to see whether your users input the url as stackoverflow.com, and if they do, change it to http://stackoverflow.com before setting the href attribute.

Didn't get your other question about programmatic navigation, but if you want a link to open in a new window/tab, use

<a href="http://stackoverflow.com" target="_blank" />

Upvotes: 3

Related Questions