In my theme, when a topic show page is loaded, I need to get the id of that topic (and name too, if possible, but id would be sufficient).
I then use this information in my theme to make an API call to get information about the topic (with a call to an endpoint like
How can I get the id of the topic when the topic show page loads?
The obvious methods are not sufficient
The obvious solution is window.location.href or window.location.pathname to get info from the url. The problem is that the URL for the topic show page can take different forms. Often, it would be:
If that was always the form, then using window.location.pathname would work well–giving me “t/topic-name/topic-id”.
BUT, sometimes the topic show page url appends the topic’s category order too, so the url is:
So using window.location.pathname is insufficient, because I don’t know programmatically whether the last param will be the topic-id or the category-index.
There may be a way to solve this using regex, but that requires some serious regex skill. There may also be a way to solve this with jquery by looking at elements on the page that might give the id–but I haven’t gotten that to work yet.