Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using some JavaScript that can only be used in IE. Is there a way to hide my custom ribbon button if not in IE? Or only display it if it is IE.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The simpliest and recommended way: paste javascript: return navigator.appName == 'Microsoft Internet Explorer'; into your EnabledScript parameter of CommandUIHandler element. This will not hide the button, but disable it.

AFAIK, where is no legal way to hide a ribbon button based on client-side condition. Anyway, you can do this using DOM. You will need to hide an element with Id, equal to your button ribbon id + "-Large". For example, if your button has Id="Ribbon.Documents.New.MyCustomButton", you will need element with id "Ribbon.Documents.New.MyCustomButton-Large".

Final CommandUIHandler tag code will look like this:

<CommandUIHandler
    Command="MyCustomButton-Click" 
    CommandAction="javascript:myCustomButtonAction-IEOnly();"
    EnabledScript="javascript:if (navigator.appName != 'Microsoft Internet Explorer') document.getElementById('Ribbon.Documents.New.MyCustomButton-Large').style.display = 'none'; return true;" />

But even if it is possible, I don't really recommend you to use this code, because it is a kind of hack, and where are some scenarios, when this solution will not work.

For example, if you insert your button into standard ribbon button group, you will have to use standard control group template for it. And these templates are resizable: this means, that if you resize your browser window, their size could change accordingly. So, the "-Large"-postfixed elements will be hidden, and, for example, the "-Middle" ones will be shown. Ok, we can hide "-Middle" also, can't we? But if user will resize his browser window back, you "-Large" button will be shown out again.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.