Monday, September 8, 2008

Retain Last working Tab between Page Saves

A former employee and a dear friend asked me today if there is a way to retain the last working tab between saves on a specific entity window.


He suggested cookies, which was the obvious choice, but I needed a way to differentiate between windows. The problem is that when the window reloads it loses its state and I was searching for an anchor on each window. Of course there are other options which you might use like saving the last tab on the opener window or save it on the server but that didn’t seem right. The opener can be closed at any time and saving it on the server is an over kill and requires me to handle pages per user.


I did some digging and found that after the page is saved (crmForm.FormType=2) the window name is retained between saves and so I can use that as the basis for a cookie name and extract it when the page loads. I tested it on IE7 and it seems to work smoothly. The cookie expires when the navigator session is closed.


Paste the code inside each entity onload event.
If you’re having any issues let me know (Comment).


// JScript File


function OnCrmPageLoad()
{
if( crmForm.FormType == 2)
RetainTab();
}

function RetainTab()
{
crmForm.attachEvent( "onsave" , OnCrmPageSave );
var regTab = new RegExp( window.name + "=(\\d)","gi");
regTab.exec(document.cookie);
var tab = document.getElementById("tab"+RegExp.$1+"Tab");
if( tab ) tab.click();
}

function OnCrmPageSave()
{
var crmTabBar = document.all.crmTabBar;
for(var i = 0 ; i < crmTabBar.childNodes.length ; i++)
{
if( crmTabBar.childNodes[i].className.indexOf("ms-crm-Tab ms-crm-Tab-Selected") != -1 )
{
document.cookie = window.name+"="+i;
break;
}
}
}

OnCrmPageLoad();

2 comments:

Anonymous said...

Excellent! Thank you very much!

Rusty said...

I have been searching for this, for such a long time!

It works a treat! Thank you for sharing