I wrote a simple script to add a floating button to a series of pages that redirects to a URL depending on some features. This is added via Tampermonkey.
The following produces a button in both Chrome and Firefox but it is only clickable in Chrome:
var html = '<div id="gmSomeID"><button id="tapButton" type="submit"><a href="'+output_url+'" style="color:white">A</a></button></div>';
$("body").append(html);
Any ideas what may be driving the issue?
That code is semantically ambiguous and invalid HTML; don't code that way!
Consider:
var output_url = '#';
var newHtml = `
<div>
<button id="badButton" type="submit">
<a id="badLink" href="${output_url}">Click me</a>
</button>
</div>
`;
$("body").prepend (newHtml);
$("#badButton, #badLink").click ( zEvent => {
console.log ("Clicked: ", zEvent.target.id);
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
What do you expect would happen?
If you run that code and click, then currently you get:
Clicked: badLink
.Clicked: badButton
.Because it's invalid HTML, browsers are not guaranteed to handle it in a uniform way.
So:
type="submit"
unless the button is inside a form and really meant to submit it. Always specify type="button"
by default, to avoid unexpected behavior.