cancel
Showing results for 
Search instead for 
Did you mean: 

Getting a message to appear as soon as the SAPUI5 page loads

former_member194198
Active Participant
0 Kudos

Hi All,

The example shows how to get a message to appear when a button is clicked. I'd like a message to be triggered when the page loads so that the user is greeted with the message.

I've tried the following. When the line in bold is commented in, I get a javascript error.

The button works fine though.

<script id='messageBar' type="text/javascript">

   var oMessageBar = new sap.ui.commons.MessageBar('messageBar');

   oMessageBar.setMaxToasted(3);

   oMessageBar.setMaxListed(7);

   // attach and position it to some element in the page

   oMessageBar.setAnchorID("messageBarArea");

   oMessageBar.setAnchorSnapPoint("begin top");

      // create simple Messages

      var success = new sap.ui.commons.Message("success", {type:sap.ui.commons.MessageType.Success, text:"Success short text"});

      var aMeliMelos = new Array();

      aMeliMelos.push(success);

     

      //Comment the following line in and boom!.

     // oMessageBar.addMessages(aMeliMelos);

    

   // attach them to some buttons in the page

     var addMeliMeloButton = new sap.ui.commons.Button("addMeliMeloButton");

     addMeliMeloButton.setText("Add 3 melimelo");

     addMeliMeloButton.attachPress(

       function (oControlEvent) {

        oMessageBar.addMessages(aMeliMelos);

       }

     );      

     addMeliMeloButton.placeAt("addMeliMelo");

  </script>

</head>

<body>

<div id="messageBarArea"></div>

<table>

    <tr><td id="addMeliMelo"></td></tr>

</table>

<div script="messageBar"></div>

Cheers

Richard

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Richard,

Change

oMessageBar.addMessages(aMeliMelos);

with

window.onload = function addMessage() {

    oMessageBar.addMessages(aMeliMelos);

}

I hope this is what you meant

Regards,

R. Wuyts

Former Member
0 Kudos

Hi Richard,

The most easy would be to work with the MVC-paradigm and to put a welcome message in the onAfterRendering() function.

Regards,

Nicolas

former_member194198
Active Participant
0 Kudos

Is there a way without using the MVC paradigm?. I've got a series of JSP pages with SAPUI5 as the presentation. It seems to me this is a bug. You can't add messages using the inline javascript. You can only add them during events.