Open external url and trigger action on single click?

Hello! I’m in need of a little bit of help.

I would like to open an external url and perform an action (add a tag to current tiddler) on a single click.

I started with a button and have come to this:

<$fieldmangler>
	<$button message="tm-add-tag" param="myTag">
		Add myTag
	</$button>
</$fieldmangler>

This works, that is, it adds myTag to current tiddler. Why <$action-setfield $field="tags" $value={{{ [<currentTiddler>tags[]append[myTag]] }}}/> inside the button doesn’t work if the tiddler already has some other tags bugs me, but that’s a secondary question.

Now how do I get the button to also open an external url when clicked? The to parameter of button widget works for tiddlers only, using to="https://www.example.com" makes it open a tiddler https://www.example.com.

I tried wrapping contents of button or the whole button widget in <a>. In the second case i see the link url pop-up when hovering over the button and I can middle-click it to open in a new tab, but a left click triggers only the button (and its action), doesn’t open the link.

@vilc I don’t yet know the full story but I picked up some time ago that the button widget is effectively a valid html onclick actions. This test demonstrates this working in way that should solve your problem;

\procedure do-actions()
<$action-log message="my message"/>
\end
<$button tag=div actions=<<do-actions>> >
[[https://tiddlywiki.com/#]]
</$button>
  • notice how I used the button tag=div to hide the button
  • Move your `message=“tm-add-tag” param=“myTag” into an action send message in the do-actions
  • You may still need the fieldmanglar but it may work inside the do-actions

This also works;

<$button tag=div actions=<<do-actions>> >
<a href="https://tiddlywiki.com/#ActionLogWidget" target=tiddlywiki2>~ActionLogWidget</a>
</$button>

Note the ~ActionLogWidget to stop it opening a local tiddler.

1 Like

Thanks @TW_Tones !

So I guess in the worst case a custom JS can be used to achieve what I want?

I tried both of your examples but I end up with the same what I described: the external link is there (show up when hovering, can be opened with middle click or right click > open in new tab) but a simple click only triggers the button action, does not open the external link.

Here’s some context, if it helpful.

I want to create a view toolbar button that adds the current tiddler as an event to Google Calendar. A specially constructed URL brings up the event creation interface if one is logged in to Google account. The tiddler title, text, and any other chosen info can be encoded in this URL. This part I have figured out.

Now I thought it would also be nice in some way to indicate in the tiddler that a calendar entry has been created for it. Regardless how (tag, field), it will require some TW action and button.

I was going to share the whole trick here when ready, but I got caught in the second part.

I don’t understand why, I just demonstrated it can be done? I tested it.

  • maybe something else is wrong in your wiki?
  • Try my examples and your code on tiddlywiki.com to see if it works there.

I tried the example in the google link you shared;

<$button tag=div actions=<<do-actions>> >
<a href="http://www.google.com/calendar/event?action=TEMPLATE&dates=20211001T100000Z%2FT010000Z&text=Example%20event&location=Office&details=This%20is%20a%20description%20of%20an%20example%20event.%20" target="_blank">gcal</a>
</$button>

Both the tab opened and the action was executed.

I would be happy to help you until the end because I could use the same facility, however its getting late for me tonight.

1 Like

I got it now with the following code:

\procedure do-actions()
<$action-sendmessage $message="tm-add-tag" $param="Calendar"/>
\end

<$fieldmangler>
<$button actions=<<do-actions>> >
<a href="http://www.google.com/calendar/event?action=TEMPLATE&dates=20211001T100000Z%2FT010000Z&text=Example%20event&location=Office&details=This%20is%20a%20description%20of%20an%20example%20event.%20" target="_blank">gcal</a>
</$button>
</$fieldmangler>

I tried it with <$button message="tm-add-tag" param="myTag"> first and it didn’t work. It does however if the actions are defined in a variable and then <$button actions=<<do-actions>> > is used as you have shown it. The tag=div is only aesthetical, it works without it as well. The $fieldmangler is necessary.

So in the end it turns out my idea with having <a> inside the button was right, but for some reason it works if the actions are defined as a variable <$button actions=<<do-actions>> >, but not if the message is defined directly like <$button message="tm-add-tag" param="myTag">.

Many thanks @TW_Tones ! I will share the ready thing after I tidy it up a bit later today (or tomorrow for you).