The ProblemWhen you create a web part within SharePoint, whether its a Content Editor Web Part or a Data Form Web Part, you may wish to include a button that has a redirect action. This redirect action could be static or dynamic, they key is that you wish to use something like:
if (possible code that determines which link to use...)
window.location.href = '/SitePages/MyPage.aspx';
<button class="dashboard-button" onclick="goToLink();">Go To Link </button>
This very simple example looks like it should work every time, and if it is done outside of SharePoint on its own web page, it does.
If however you embed this code within a SharePoint web part, it will sometimes work. Some users will be able to click the button and have it redirect the first time. Some users will have to click the button multiple times, each time resulting in a page refresh, before the button works. This type of intermittent issue can also be very challenging to troubleshoot.
The SolutionFortunately, this one is a really simple solution. Simply add the button type to your button markup:
<button type="button" class="dashboard-button" onclick="goToLink();">Go To Link</button>