I would like to know if is possible to set and show a apex:pageMessages block from javascript

enter image description here

I've been trying to do something like this:

VF page:

<apex:pageMessages id="jserror"/>

 var msgEl = document.getElementById("{!$Component.jserror}");
 msgEL.style="display:block;"; //sample of showing
 // here I keep doing more cool stuff

However, I have two problems:

1 - The pageMessages block is not build on page If i don't call ApexPages.addmessage(new ApexPages.message(..... So I can't find the element and show it. Obviously the message couldn't be visible by default.

2 - It's not easy to manage the message setting since the html structure change for different devices rendering(Table for desktop, Div's for mobile)

Finally, There is some workarround to manage the standar SF message from javascript? something like: sforce.visualforce.showMessage(WARNING,'This would be really cool indied!');

1 Answer 1


Instead of trying to style and alter the pageMessages markup, I would just create an action function. You could do something like:


<apex:actionFunction name="showMessage" action="{!showMessage}" rerender="jserror">
  <apex:param name="errorLevel" assignTo="{!errorLevel}" value="" />
  <apex:param name="messageName" assignTo="{!messageName}" value="" />
  showMessage('WARNING', 'This would be really cool indeed!');


public String errorLevel {get; set;}
public String messageName {get; set;}
public void showMessage() {
  if(errorLevel == 'WARNING') {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning, messageName));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .