Cancel the default action of the link.
If you are using intrinsic event attributes, return false from them.
onclick="gotopage2(); return false;"
If you are writing modern code, call preventDefault
on the event object.
<a href="http://www.page2.html">page 2</a>
<script>
function goToPage(evt) {
evt.preventDefault();
window.location = this.href;
}
document.querySelector('a').addEventListener('click', goToPage);
</script>