Creating and clicking on a Link, programmatically

One of the amazing things about Anvil’s Javascript integration is that the amount of Javascript you need to customize it is often minimal. I know very little Javascript myself, and have been able to do whatever minimal Javascript I need to allow me to continue working in Python.

To programmatically open a link via Javascript requires very little code. Something like this in your Native Libraries section:

function open_url_same_tab(url) {
  window.open(url, "_self");
}

And then your Python code to open the link becomes:

js.call_js('open_url_same_tab', self.link.url)

That was adapted from this post: How to make a link open in the current tab You’ll find a clone example of the technique there, too.

4 Likes