Dynamic Browser Window Title - Take 2

Jim Coyne
Kilo Patron

I just wanted to add some functionality to the custom window title script I wrote about in this blog post - Dynamic Browser Window Title.   I cannot edit the post so I'll add a new one here.

 

dmolnar101 added some code for the First and Last names on the User form and then asked about displaying the Knowledge base article's name.   So I incorporated the First/Last name fields as well as the Knowledge Base enhancement and a few other goodies.

 

The title will change to "Knowledge Base" if you are looking at the KB homepage and then will change to "KB:" + the article's Short description when looking at a specific article.   Will also display "Service Catalog", "Survey" or the name of a properties page.   Works in both the regular UI as well as the CMS.

 

Here's the code:

addLoadEvent(u_setCustomTitle);
function u_setCustomTitle(){
     var defaultTitle = "Custom Window Title";
     var title = defaultTitle;
     //check for a window name first
     var windowName = top.window.name.toLowerCase();
     switch (windowName) {
           case "workflow":
           title="Workflow Editor";
           break;
           case "show_workflow_context":
           title = "Running Workflow(s)";
           break;
           case "super_bsm_map":
           title = "BSM Map";
           break;
           case "super_schema":
           title = "Schema Map";
           break;
           default:
           try {
                 //grab some "normal" field values if a window name was not found
                 var name = g_form.getValue("name").replace("undefined","");
                 var firstName = g_form.getValue("first_name").replace("undefined","");
                 var lastName = g_form.getValue("last_name").replace("undefined","");
                 var number = g_form.getValue("number").replace("undefined","");
                 var shortDescription = g_form.getValue("short_description").replace("undefined","");
                 if ($("sysverb_query") != undefined) {
                       title = "Search";
                 } else if (g_form.isNewRecord()) {
                       title = "New Record";
                 } else if (name) {
                       title = name;
                 } else if (firstName && lastName) {
                       title = firstName + " " + lastName;
                 } else if (firstName) {
                       title = firstName;
                 } else if (lastName) {
                       title = lastName;
                 } else if (number && shortDescription) {
                       title = number + " - " + shortDescription;
                 } else if (number) {
                       title = number;
                 } else if (shortDescription) {
                       title = shortDescription;
                 }
           } catch(err) {}
           if (title == defaultTitle) {
                 //maybe we are in the Knowledge Base view?
                 try {
                       if ($("kb_view")) {
                             title = "Knowledge Base";
                             var articleName = "";
                             articleName = $$('div[class="kb_article_header_short_description"]')[0].innerHTML;
                       }
                       if (articleName) {
                             title = "KB:" + articleName;
                       }
                 } catch(err) {}
           }
           if (title == defaultTitle) {
                 var caption = "";
                 //still nothing???   Maybe the Service Catalog or a Survey
                 try {
                       caption = $$('div[class="caption"]')[0].innerHTML;
                       if (caption.indexOf("Service Catalog") > -1) {
                             title = "Service Catalog";
                       } else if (caption.indexOf("Survey") > -1) {
                             title = "Survey";
                       }
                 } catch(err) {}
           }
           if (title == defaultTitle) {
                 //Really??????   Keep digging, it might be a properties page?
                 try {
                       caption = $$('span[class="caption"]')[0].innerHTML;
                       title = caption;
                 } catch(err) {}
           }
     }
     top.document.title = title;
}

 

I've attached the XML as well so you can just import it.

9 REPLIES 9

Shawn Dowler
Tera Guru

i really like this idea, but is there a reason why this is being done on the client side rather than the server side? I'm working on making public knowledge articles open to Google crawling and a Google Search Appliance. Using Javascript to modify the page titles, unfortunately, doesn't do anything to change the page titles that appear in search results. When it becomes a priority, I will be trying to modify this script to work in a UI Macro that will modify the <title> tag before the HTML is output.


I was able to do what I was looking for in a way that works for KB articles and resolved my issue of using a Google Search Appliance to index articles from ServiceNow with proper titles. The answer is here: RP is not Defined in UI Macro


torwc
Kilo Contributor

Hi


This script is very usefull and works like a charm.


However there is an issue when I open a record in a new tab.


For example in the Incident Application menu I right click on Open and select 'Open link in New tab'.


Then from the list I select an indident.


The browser window title is now "INC0001234 | INC0001234 - my short description"



Is there any way I avoid the duplicate number?


SN is doing something extra when the page is not within the normal frame.   You could just drop out of the script if it is in a separate window/tab (add lines 7 - 10 from below) and let SN do its own thing:



addLoadEvent(u_setCustomTitle);


function u_setCustomTitle(){


  //drop out if in the CMS Portal


  if (window.top.location.toString().indexOf("service-now.com/ess/") > -1) {


        return;


  }


  //...or in a separate window/tab


  if (window.name== ""){


        return;


  }


  var defaultTitle = "Custom Window Title";


  ...


  ...