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

torwc
Kilo Contributor

Thanks Jim for Your help.



//Tor


You are welcome.


akaupisch
Kilo Guru

I've taken this a step further in Helsinki... awesome work btw...The issue with Helsinki is that SN updates the title after all the onLoad events take place...As such, your script was executing but being overwritten shortly after it was executed. Adding the following logic fixes it...naturally you can set the timeout to be whatever you'd like, I have this in there since there are times where a full refresh of the browser can take 3-5 seconds to load...The end user might notice the SN code being executed, but it would be for less than 1second...



addLoadEvent(doWait);




function doWait()


{


  if (document.readyState !== "complete")


            setTimeout(function(){doWait();}, 500);


  else


            doSomething();


}




function doSomething()


{


//insert code here


}



I've attached the code here....it's a consolidated and updated version of what is above


Thanks for the update.


This works great for me using a UI Page opened in a new tab on its own as well within a SN frame. However, when opened within the SN frame, the title changes back to "ServiceNow" immediately when I go to a different tab. The title remains on the solo page on its own in a new tab.



Any ideas why?