Knowledgebase/Help Center Redirection using JavaScript

5 Comentários

  • Nicole - Community Manager

    Thanks for sharing this tip and putting all of this together, Scott!

    0
  • Jennifer Rowe

    Scott--another awesome tip from you!

    Thanks so much for sharing this. I love that you always include a helpful video!

    This will help a lot of users! I've added it to our Guide tips master list.

    0
  • Eivind

    I am currently using a javascript snippet to do this, but it is not in the document_head.hbs file. Is there any advantage to having it in the document_head.hbs file versus the script.js file? 

    1
  • Scott Havard

    Hey Elvind,

    Seeing that both document_head.hbs and script.js inject into every page, I don’t think one is the better way (I am sure a front end engineer would have an opinion, but my knowledge is limited!).

    Now, the one problem I hit was my redirects wouldn’t work for me if I had the code in script.js. A zendesk authentication script ran before the redirect, so users would be redirected to a login page and not the right article.

    You can double check that by testing your redirect in an incognito window. If they work fine there, then I don’t see why you would move them out of script.js.

    Let us know what you find!

    0
  • Eivind

    Hi Scott,

    I am surprised that redirects didn't work for you if you had the code in script.js. Do you have some custom code in script.js that runs first?

    Mine works in incognito without problems.

    My script is based on Joe Federic's script here: https://support.zendesk.com/hc/en-us/articles/217958367/comments/360000165468

    I have modified it to have variables for URLs and it builds URLs. It works, but I think it may be less efficient than yours since I am using loops.

    Here's the example code:

    // Redirecting old articles to new articles or redirects

    //Define variables for support centre URL redirects, will be used below to build complete redirect URLs

    var mainHelpCentreURL = 'https://support.example.com/hc/en-us/';
    var helpCentreArticles = 'articles/';
    var helpCentreCategory = 'categories/';
    var helpCentreSection = 'sections/';

    var secondaryHelpCentreURL = 'support2.example2.com/hc/en-us/';

    //Use this for redirecting articles to articles
    var one = {
    "1" : "2",
    };

    //Use this for redirecting to sections
    var two = {
    "1" : "2",
    };

    //Use this for redirecting to categories
    var three = {
    "1" : "2",

    };

    //Use this for redirecting to landing page

    var four = {
    "203325983" : "",
    }

    //Loops for redirects

    //Loop for redirecting to articles

    for (var keyone in one){
    if (window.location.href.indexOf(keyone) > -1){
    window.location.replace(mainHelpCentreURL + helpCentreArticles + one[keyone]);
    }
    };

    //Loop for redirecting to sections

    for (var keytwo in two){
    if (window.location.href.indexOf(keytwo) > -1){
    window.location.replace(mainHelpCentreURL + helpCentreSection + two[keytwo]);
    }
    };

    //Loop for redirecting to categories

    for (var keythree in three){
    if (window.location.href.indexOf(keythree) > -1){
    window.location.replace(mainHelpCentreURL + helpCentreCategory + three[keythree]);
    }
    };

    //Loop for redirecting to landing page

    for (var keyfour in four){
    if (window.location.href.indexOf(keyfour) > -1){
    window.location.replace(mainHelpCentreURL + four[keyfour]);
    }
    };

    // End of code for redirecting
    1

Por favor, entrar para comentar.

Desenvolvido por Zendesk