سؤال

I have a message page written in jsf and in this page i have a table containing message titles as commandlink. In these commandlinks i have nested ajax to request to my bean to read the message content from db and show it on the page. I tested this functionality by showing the message content in a textearea. But the problem is here that i want to show the content in a javascript alert. So whenever I click the commandlink an alert shows the content of the message. But if I define the javascript method as commandlink onclick attribute then it would run before ajax request and nothing would be shown! I think we need an attribute that defines a javascript method that would run after ajax request completion. But what's that attribute ? thanx for any useful answer !

هل كانت مفيدة؟

المحلول

XHTML

<h:commandLink id="button" value="button">
        <f:ajax render="@this" 
            onevent="openPopup"
            listener="#{myBean.getData}" />
</h:commandLink> 

Javascript

function openPopup(data){
  if(data.status == "success"){
      // open popup with your message
  }
}

نصائح أخرى

new to XMLHttpRequest? try play around with this code

<!DOCTYPE HTML>
<html>
<head><title></title></head>
<body>
<input type="button" id="foo" value="load url" /><br/>
<input type="text" id="url" /><br/>
<textarea id="responses"></textarea>

<script type="text/javascript">
document.querySelector("#foo").addEventListener("click",function(ev){
var xhr=new XMLHttpRequest();
var url=document.querySelector("#url").value;
xhr.open("GET",url);
xhr.addEventListener("readystatechange",function(ev){
var xhr=ev.target;
if(xhr.readyState<4)return;
document.querySelector("#responses").value+=xhr.responseText;
alert(xhr.responseText);

});
xhr.send();
});
</script>
</body></html>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top