Tip: Finding current page type (category, section or article) and id
This is how you can find out the page type (category, section or article), and the corresponding (category, section or article) id:
/**
* Finds current page type (categories, sections or articles)
* and corresponding id
*
* @return {Object} Returns object with 'type' and 'id' properties
*/
function getPageInfo () {
var _t = window.location.href.split('/hc/');
_t = _t[1].split('-')[1].split('/');
return {type:_t[1], id:_t[2]}
}
//USAGE:
//var page = getPageInfo();
//var pageType = page.type;
//var pageId = page.id;
//console.log(page.type);
//console.log(page.id)
Cheers,
Diziana (Free and Premium Zendesk Themes and Plugins)
-
Thanks for sharing this, Diziana!
-
I'd be really curious if this is the "best way" to do this. It would be preferable for zendesk to expose this from a helper. Obviously, if they change their URL format/positioning, this whole thing breaks. So a year or two from now you find yourself with code that's breaking. I mean, it's easy enough to probably debug that, but might leave your page in a crap state until someone figures out things are broken.
All said, thanks for sharing the snippet Diziana.
Hopefully a Zendesk employee can chime in on my question.
-
Also, if it's the "home page", you won't get the "type" in that array, so I had to hack this in.
What's even more troubling is this won't likely work on some other page type I'm not yet aware of being somewhat new to Zendesk (e.x. I already see that the "contributions" template will falsely put type of 'home' the way I've coded this).
I've submitted a ticket and will post back if I find a better solution from the Zendesk folks.
function getPageInfo () {
var _t = window.location.href.split('/hc/');
_t = _t[1].split('-')[1].split('/');
if (_t.length < 2) {
_t[1] = 'home'; //Hack: fudge in home page
}
return {type:_t[1], id:_t[2]}
} -
Ok, ZD got back and it doesn't appear that there's an available global/advanced helper for this so that hack is probably only way for page type. That said, it's probably better to use something like article.id, section.id, if you KNOW you're on a corresponding page. In my case, I only really care about the page type so hack stays ;)
-
Thanks for the update, Rob!
-
Yeah, one last thing I got back from ZD which is at least worth considering:
"For each Template type, you modify the code to suit that page. So, for example, on the Home page, you use:
function getPageType() { return "home"; }
So you do that for category, section, etc., and then you're guaranteed it's correct without URL sniffing. But, you have more to maintain and have to include quite a bit of JS in your templates. So it's sort of "six and half dozen one or the other"; maintainence vs. future proofing. That said, probably worth having here for folks wanting to consider options...I'm going to stick with hack #1 URL Sniff. Of course support was quick to put disclaimers on all of these hacks as is not surprising.
End of day, my thought is that this would be a great thing for ZD to expose via a helper - jm2c :)
-
can't zendesk just add a class to page template body tag?
-
It's very sad to see how the template mechanism is poor.
Almost everything should rely on "hacks", especially in javascript if you really want some dynamic content
-
Hey Arnaud -
I'd encourage you to share your feedback with our product managers by posting it in the Guide Product Feedback topic. Here's info on how to write an effective feedback post.
-
Rob, If I wanted the section info, how might I go about it using this code? Specifically, I need to determine whether the article exists within a specific section.
Please sign in to leave a comment.
10 Comments