1

I want to navigate from my website to another website using a button, here is my code, I am using React Bootstrap with this project

<button onclick="window.location.href = 'https://www.glassdoor.com';">Click Here</button>

However, when I click it, it does not navigate to the new website, it only reloads the homepage of the website I am on.

je1013
  • 21
  • 5
  • Does this answer your question? [The page will strangely refresh when I click the button](https://stackoverflow.com/questions/44681646/the-page-will-strangely-refresh-when-i-click-the-button) – Emile Bergeron Apr 30 '20 at 18:20
  • 1
    Also, since you're using [Bootstrap, just use an anchor tag](https://getbootstrap.com/docs/4.0/components/buttons/#button-tags): `Click` – Emile Bergeron Apr 30 '20 at 18:22

1 Answers1

3

window.location.href returns the href (URL) of the current page

So you can remove the href part and simply set the value of the url you wish to navigate to.

    <button onclick="window.location = 'https://www.glassdoor.com'">Click Here</button>

Failing that you can also use the function assign

<button onclick="window.location.assign('https://www.glassdoor.com')">Click Here</button>
Fraser
  • 15,275
  • 8
  • 53
  • 104
  • Thank you! I am getting an issue with both solutions though. If I put quotation marks around the whole function, the button does not do anything, however, without the quotation marks, it executes automatically without the button being clicked – je1013 Apr 30 '20 at 18:29
  • post updated code please. The above should work fine. I recommend the `window.location =` – Ryan Deckard Apr 30 '20 at 18:49
  • 1
    I found the problem with my code, some syntactical problems. Thank you very much! – je1013 Apr 30 '20 at 18:51