[Fixed] Clicking a link element doesn't open the URL anymore when an onClick event is set

What I’m trying to do:

When the user clicks on a link element 2 things should happen:

  1. The website specified in the url of the link opens.
  2. The onClick event of the link element is executed.

This used to work until today, now only #2 happens.

What I’ve tried and what’s not working:

Clone link:
https://anvil.works/build#clone:FVYVWASC7YRR27RQ=VJH33ASKTGY5SB5YEVWIJKHG

Same thing here, an app that was working for over a year just stopped functioning at least yesterday.

Its entire purpose is to generate a file for download by low skilled users, and clicking the link no longer downloads a file.

Firefox, Edge, and Chrome in the IDE (not beta).

click_no_work

This worked last Friday, nothing changed in this app for months.

1 Like

I dug into it it looks like I’m setting a function to run when the ‘click’ event is fired on an on the fly code generated component (A link component). The change seems to be that the link no longer functions as a link when click is also used.

Until just a few days ago, clicking the link actually clicked the link, then fired your assigned event code.

It seems to no longer do that.

1 Like

I assume this was done in error. Seems like a pretty big change for a silent modification.

I just found a work around for my case, add this into your click event to make it download a url for a file:

anvil.media.download(anvil.URLMedia(event_args['sender'].url))

However @tomka , I tried something similar in a clone of your app and can’t even get this to work:

    temp_link = Link(url=anvil.URLMedia(event_args['sender'].url))
    temp_link.raise_event('click')

Even using raise_event with click without changing or even making a new click event for a new link component somehow stops it from navigating to the link.

My temporary solution has been to show a popup with clickable links instead. Not great but works reliably.

You can use js.window.open(URL, '_blank') to open a URL in a new tab from code. That at least should give you a workaround for now.

1 Like

Thanks @jshaffstall , mixing that all together leads to this workaround:

js.window.open(event_args['sender'].url, '_blank')

If you put this somewhere in the click event you created for the link, it will navigate to the URL in the link at whatever line in the event code you put it.

again if someone is reading it and missed the posts above, the workaround for getting it to download from a link is:

anvil.media.download(anvil.URLMedia(event_args['sender'].url))
3 Likes

Thanks, Jay! @jshaffstall

Ian: @ianbuywise
Small addition: There seems to be a bug in Safari that prevents media objects that are not stored in a database from being downloaded. Workaround for that is to temporarily store the object in a database: Error downloading PDF report - #6 by mglraimundo (The workaround is implemented in the demo app linked by @mglraimundo .)

1 Like

Apologies for this - a fix for a different bug introduced this one.
This should now work as expected.
Thanks all for the helpful work arounds.

1 Like

Actually, I need to click twice. Only on the second click will the url open. Before that only the click event is executed.

1 Like

In other words, it still doesn’t work.

For each link I have to click once (= my temporary popup is shown). After that when I click both the popup is shown and the download starts.

I tried your first clone and it worked as expected. Can you provide a new one that demonstrates the issue?
Or more information on how to reproduce including which browser/os.

Nevermind, I got it to work now in my production app as well. For some reason I have to set the url again when showing the form, then it works fine. This wasn’t necessary before, but it’s no big deal. Thank you for your help!