Welcome to the IBM Collaboration Solutions Community IQJam.
UsernamePassword
Reset Password | Register
   
Home | Leader Board | Tags | Help
Custom Control addOnLoad fires only one time? 
I am trying to create a re-usable custom control. A javascript function needs to trigger when this custom control is partial refreshed by a button on the container XPage. I don't want to write code outside the custom control (else I could have coded the javascript on the onComplete event of the button).
For that reason, I wrote the xsp.addOnLoad on the custom control. Unfortunately, this function fires only the first time. Does anyone know of a solution to this. I want this function to fire every time the button is clicked.
 
Thanks, 
Rajeev. 
Domino Development / XPages
Rajeev Menon - about 1 year ago |  |  | Viewed 212 times

There are 9 answers

0votes
Is the XSP.addOnLoad() defined within the area that gets partially refreshed?  If it is, it should get executed every time. Do you have some sample code you could post?


Jeremy Hodge - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
I gave the name (id) of the custom control after adding it to the XPage. A button on the XPage refreshes this Custom Control on click. Just to make sure that the custom control is getting refreshed every time, I put a computed field with @Random() as the value in the Custom Control. I see that the random number is generated every time I click the button. But the onload is firing only the first time I click the button.
 
I don't have the addOnLoad code now. But I will post it tomorrow. Thanks again Jeremy. 


Rajeev Menon - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
Rajeev,
 
 
addOnLoad only happens when the user opens the document, but not during partial refreshes.

You need to move your code to beforeRenderResponse.


John Smart - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
Hi John,
 
That cheat sheet is for the server side events. The question I have is about the javascript event.

<script language="Javascript">

XSP.addOnLoad(function(){alert(1)})

</script>

But I am starting to have an impression that addOnLoad is supposed to fire only once. Well, then is there any other events or any other way to trigger a piece of javascript code from within the custom control after the partial refresh is done.

The XSP in XSP.addOnLoad kind of tells me that the onLoad will fire only once (when the XPage is actually loaded initially)  



Rajeev Menon - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


1votes
@John .... XSP.addOnLoad is different than the JSF lifecycle events detailed in the XPage Cheat Sheet .... XSP.addOnLoad is a client-side javascript function ... IF the script is within the region that is getting partially refreshed, it will fire every time.  There is code in the XSP object that evaluates and executes scripts when a partial refresh is done, and will trigger the XSP.addOnLoad scripts ... (where dojo.addOnLoad will not fire on partial refreshes) ... the key here is the XSP.addOnLoad needs to be sent with the partial refresh every time.  If that is not possible, see this blog post on how to watch for a partial refresh event and execute code after it executes.


Jeremy Hodge - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


1votes
I figured out a way to do this. I added a Computed field on to the Custom Control with the content-type as HTML, with the following as the value. It works every time the control is partial refreshed.

"<script language=Javascript>alert(100)</script>"



Rajeev Menon - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
Rajeev: Oops.  Sorry.
 
Jeremy: Nice blog post on the partial refresh event and how to hook into it.


John Smart - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


1votes
Rajeev,
 
You can also use an output script control ... In your XPage, where you want the script located, click the Create menu, and pick Other, then expand Core Controls and pick Output Script ... this allows you to embed a client side script using a script editor, etc ... and you don't have to fake it with the text control.


Jeremy Hodge - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community


0votes
Hi Jeremy,
 
I wrote the JS function on the OutputScript and then called the function using the Computed Text.
 


Rajeev Menon - about 1 year ago | 
Voting
Vote on the answer to show whether you think the answer is correct or useful to the rest of the community.

Answers with more votes are more visible to the rest of the community