Skip to content

event.preventDefault() not working when clicks <a> elements #2791

@xuchaoqian

Description

@xuchaoqian

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/):

<a href="http://stackoverflow.com/q/8614438/1366033" 
   onclick="event.preventDefault()" 
   target="_blank">click</a>
<a href="http://stackoverflow.com/q/8614438/1366033" target="_blank" >click2</a>
<a href="http://stackoverflow.com/q/8614438/1366033" target="_blank" >click3</a>
<script>
  const links = document.links;
  for (let i = 0, n = links.length; i < n; i++) {
    links[i].onclick = function (event) {
      event.preventDefault();
      return false;
    };
  }
</script>

Expected behavior

Should NOT open the link in the default browser.

Platform and Versions (required):

Operating System - Mac OS, version 11.6.0 X64

Node.js environment
  Node.js - 14.18.1
  @tauri-apps/cli - 1.0.0-beta.10
  @tauri-apps/api - 1.0.0-beta.8

Global packages
  npm - 6.14.15
  yarn - 1.22.17

Rust environment
  rustc - 1.56.0
  cargo - 1.56.0

App directory structure
/bin
/node_modules
/webpack
/public
/src-tauri
/build
/.git
/.vscode
/src

App
  tauri.rs - 1.0.0-beta.8
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../build
  devPath - http://localhost:9091
  framework - React
  bundler - Webpack

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions