Making a Button click run a JavaScript snippet -OR- Open a new Tab and navigate to a URL

Hello Friends:

It’s been a while and I hope everyone is well. :blush:

I have numerous Forms created with Zoho Forms. I would like to be able to click an Anvil Button Component and run the below Form code-snippet (where I obfuscated the actual Form URL). This was generated for me when I clicked Zoho’s generate Hyperlink feature:

Embed your form as a hyperlink on a web page. The form opens in a popup window when the link is clicked.

<script type="text/javascript">function zforms_open_window(url, height, width){var leftPos = 0;var topPos = 0;if(screen){leftPos = (screen.width - width) / 2;topPos = (screen.height - height) / 2;window.open(url, null, 'width='+width+',height='+height+',left='+leftPos+',top='+topPos+', toolbar=0, location=0, status=1, scrollbars=1, resizable=1');}}</script><a href='https://example.com/XXXXXXXXXXXXX' title="Contact Us" target='_blank' onclick="zforms_open_window(this.href, 648, 700); return false">Access Form</a>

I’m guessing doing something with the Button’s Click Event will be involved, but whatever the solution, what is the easiest way to implement this. I simply want to click a landing-page Button and have the pop-out work.

EDIT:
PS: I can alternatively have Zoho generate iframe code (and other embeddable formats) if that is easier. Or, even just opening up a new Tab and navigating to that Contact Form URL would suffice; as long as the current Tab and its contents isn’t lost.

Thank you in advance! :blush:

Welcome back!

You can use Anvil’s Javascript bridge to open a URL in a new tab. Something like:

import anvil.js

anvil.js.window.open(URL, '_blank');
2 Likes

Hi @jshaffstall !

When I ran into this hurdle, I actually thought of you because you always help, and especially when it comes to non-Python topics (my weakness).

Your solution (marked as solved) worked perfectly, and is in operation. I will use it for two buttons and their respective forms. :grinning_face_with_smiling_eyes:

Thank you as always! :tada:

2 Likes