Saturday, January 16, 2010

User friendly information messages

In some situations you might want to provide the end user with some information. A way to do that is using alerts. But that is not always as user friendly as you might want.

Tanguy wrote a javascript for more user friendly messages that was improved by Si. I thought I could improve it a little further by making it more flexible and share the code with you.

Place the next function on the OnLoad event of the CRM form.

Notification = function(sNotification) {
    var elementId = 'Notifications';
    var id = 'divMessage';
    var src = document.getElementById(elementId);

    if ((sNotification == null) || (sNotification == "")) {
        src.style.display = 'none';
    }
    else {
        var newcontent = document.createElement("span");
        newcontent.id = id;

        newcontent.innerHTML = "<table><tr><td><img src='/_imgs/ico/16_info.gif' /></td><td valign='top'>" + sNotification + "</td></tr></table>";
        src.style.display = "";

        var previous = src.firstChild;
        if (previous == null || previous.attributes['id'].nodeValue != id) {
            if (src.childNodes.length == 0)
                src.appendChild(newcontent);
            else
                src.insertBefore(newcontent, src.firstChild);
        }
        else
            src.replaceChild(newcontent, previous);
    }
}

Now you can call this function from anywhere on the form. For example on the exchange of accountnumber:

if(crmForm.all.accountnumber.DataValue != null)
{
    Notification("Accountnumber = "  + crmForm.all.accountnumber.DataValue);
}
else
{
    Notification();
}

Which results in the next information message:

InformationMessage

No comments:

Post a Comment