New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
event.preventDefault() not working when clicks <a> elements #2791
Comments
Can you change your snippet to
please re-open if the issue persists. |
Also check #1657 |
Thank you for your response. It does not work, too.
But I got a workaround. Just change the target attribute of link to "_tab" or any other names not in the standard set: _self, _parent, _top, _blank.
This workaround is suitable for our own sites, but does NOT work for third-party's site. It will be better to support capturing & handling all events from html document directly. |
I had a typo in that snippet, apologies. |
Thank you so much. I will discuss this feature/bug in the other issue.
FYI:
|
Alright, thanks for reporting back. Further discussion should be in #1657 |
function setupLinks(node) {
if (node.nodeName.toLowerCase() === 'a' && node.getAttribute("target") === "_blank") {
console.log('Found <a> tag:', node);
node.onclick = function (event) {
event.preventDefault();
event.stopPropagation();
window.open(node.href, "_blank")
return false;
};
}
if (node.childNodes.length > 0) {
node.childNodes.forEach((childNode) => {
setupLinks(childNode);
});
}
}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
setupLinks(node);
});
});
});
const target = window.document;
const config = { childList: true, subtree: true };
observer.observe(target, config); |
Describe the bug
We want to capture all click events on < a > elements, and then open link in new tauri window or custom tab(just like chrome tab), instead of opening link in the default browser.
It works fine in the default browser, but gets unexpected result in tauri windows: event.preventDefault() does NOT work anymore, tauri always open it in the default browser.
To Reproduce
Steps to reproduce the behavior:
Code sample as following(See it online: https://jsfiddle.net/uvrt0ajd/5/):
Expected behavior
Should NOT open the link in the default browser.
Platform and Versions (required):
The text was updated successfully, but these errors were encountered: