Many websites offer external links to other websites. Some of them use hyperlinks:
<a href="https://example.com">Click me</a>
Others use buttons:
<button onclick="window.location.assign('https://example.com');">Click me</button>
It turns out that buttons use the window object to load pages, while hyperlinks use <a>
elements.
According to the HTML5 validator published by the W3C, the button element should not be a child of the a element.
This fails:
<button>
<a href="https://stackoverflow.com">See New Questions</a>
</button>
But window.location.assign
is the same as the a element, as shown by this snippet:
$('button').click(function() {
window.location.assign('https://stackoverflow.com');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>See New Questions</button>
<a href="https://stackoverflow.com">See New Questions</a>
So why does the W3C prefer window.location.assign()
over <a>
elements for HTML buttons?