linkifyjs icon indicating copy to clipboard operation
linkifyjs copied to clipboard

Why is `options.events` not available on e.g. `linkify-string`

Open tafelnl opened this issue 3 years ago • 2 comments

In the docs it's mentioned that options.events is available on

element, jquery interfaces only

Why is it not available on linkify-string for example. I think I have a valid use-case where this might be needed:

I have plain text input (simple chat messages), of which all but one type of urls should be transformed according to the default settings. For one specific url however, I do want a href set, but I also want to set an event listener, so I can decide for that specfic URL what to do exactly on clicking it. More specific, for example, I want to actually call vue.$router.push() when clicking this a.href element, so I can mimick the behaviour of a <router-link> element.

Is this at all possible with the current API?

tafelnl avatar Jun 29 '22 08:06 tafelnl

Hmm, yea this just bit me. I didn't understand why the event was just ignored when using linkify-string, until I found this issue. Strange.

Rican7 avatar May 02 '25 23:05 Rican7

Hi @tafelnl @Rican7, thanks for your comments here. Since linkify-string produces HTML strings, it's not possible to add JavaScript event listeners to the output unless you are constructing the HTML elements. yourself with JavaScript.

As a workaround, you can specify event handler attributes such as onclick, for example:

linkifyStr("https://github.com", {attributes: {onclick: "myEventHandler()"}})

Where myEventHandler is your event handler function that you have defined in your JavaScript somewhere.

I'm not sure how this would integrate with something like Vue, I haven't used that framework before.

Hope that helps, let me know if you have any other questions.

nfrasser avatar May 07 '25 02:05 nfrasser