Tip: Finding current page type (category, section or article) and id

10 Comments

  • Jennifer Rowe
    Comment actions Permalink

    Thanks for sharing this, Diziana!

    0
  • Rob Levin
    Comment actions Permalink

    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.

    0
  • Rob Levin
    Comment actions Permalink

    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]}
    }

     

    0
  • Rob Levin
    Comment actions Permalink

    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 ;)

    0
  • Jennifer Rowe
    Comment actions Permalink

    Thanks for the update, Rob!

    0
  • Rob Levin
    Comment actions Permalink

    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 :)

    0
  • Iggy Pritzker
    Comment actions Permalink

    can't zendesk just add a class to page template body tag?

    3
  • Arnaud Heritier
    Comment actions Permalink

    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

    0
  • Nicole - Community Manager
    Comment actions Permalink

     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

    0
  • David Bjorgen
    Comment actions Permalink

    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.

    0

Please sign in to leave a comment.

Powered by Zendesk