Hello everyone,
yes, the title is very confusing, I’ll clear that up. There are in fact a few things I’m not too sure about and reading through the entire documentation still left a few questions.
I don’t want to bring up an XY-question where I ask about Y because I think it helps me to do X, instead of just asking about X right away. I’ll explain what I want to achieve, add my thoughts about what I tried already and I’d be happy if you could suggest a better, shorter and cleaner way to achieve this if there is one and I’m sure there might be one.
So I want to run a certain JavaScript function, when you click a certain link, which is wrapped in a shortcode, but this should only be done if the shortcode declaration contains a third given parameter. For that I’d simply use {{ with .Get 2 }}.
In this JavaScript code, I want to refer to the link, this link is linking to. Additionally, I want to remove the hash (if an ID is currently mentioned in the URL) and use this to remove something from the localStorage. The JavaScript code itself shouldn’t be a problem.
I’d theoretically want to use something like this inside an inline style-tag in the shortcode HTML file (quick pseudo-code):
const hashIndex = "{{ .Get 0 }}".indexOf('#') || url.Length;
const newUrl = "{{ .Get 0 }}".substring(0, hashIndex);
localStorage.removeItem("${newUrl}-something");
but that isn’t possible due to the .Get 0 not being valid (while the first parameter is always the URL it’s referring to).
I thought about saving the URL in a meta-tag, but I’d have to loop through all occurences of this meta-tag anyway and somehow differentiate which belongs to which. I could technically give the “a”-link a special ID, but I’m not sure how, since that all is taking place inside a shortcode HTML file.
I tried a lot of things, combining a lot of different things, but I couldn’t come up with a reasonable solution.
I mean for the click function, if the “a”-link doesn’t have a unique ID, I could have an ugly workaround doing something like this:
{{ with .Get 2 }}
<a href="{{ .Get 0 }}" class="link">{{.Inner}}</a>
{{ else }}
<a href="{{ .Get 0 }}" class="link" onClick="someFunction()">{{.Inner}}</a>
{{ end }}
but that is pretty ugly and inconvenient for several reasons. Using an onClick over an addEventListener in JS might not be the preferred way anyway and this whole thing looks a little bit hacky. There would still be the problem with the link though, because I can’t include the .Get 0 as a parameter either.
So whatever I tried, I ended up encountering an obstacle somewhere.