on 2005 Sep 22 9:43 AM
Hello everyone, I have Dynpage class and there is a button has been created in this class. When this button is clicked it will trigger two events onClick and onClientClick. Now I have two functions called onSave and onClientSave.
I need the JavaScript code in OnClientSave to be executed until the Java code in OnSave is finished.
The js script has been loaded when doInitialization() is called. The js function is name test(){alert();}. I have tried to call js function by using
TextView tv = new TextView("test()");
tv.setEncode(false);
getForm().AddComponet(tv);
in doProcessAfterInput() or doProcessBeforeOutput(), but js code can not be executed.
As this class will be intergated with a Dynpage class I would not like to use JSPDynPage.
Can anyone give me a hint how to control the sequence? Thanks for early reply.
Kind regards.
Yantong Wang
Message was edited by: Yantong Wang
Yantong
I don't think that you understand the way in which the Javascript will work. ALL Javascript is only executed on the client, which means that when a button has an onClientClick method it looks for the Javascript function at the client level to execute before sending the request back to the server for processing the onClick method. Once the request has been sent back to the server you will not be able to execute any more javascript until the response has been sent back to the client, therefore putting some javascript code into the doProcessAfterInput method will actually do nothing.
What exactly are you trying to do with the onClientClick event ?
D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Darrel, thanks for reply.
In doPorcessBeforeOutput or doProcessAfterInput if I pass Boolean value false to the setEncode() of a TextView, I think I can send my JavaScript code from server to the client side.
The idea is to refresh an IFrame. This Iframe displays the result after a Java class is called. If the button is clicked the Java class should be called and finally the
Iframe should be refreshed.
But now the problem is the IFrame is refreshed before the Java class is completed.
I am working on the source code which is developed by a third party. So a big change in the code is not good idea in my case.
Kind regards.
Yantong Wang
Yantong
You need to understand the exact process of how the DynPage sends the output to the client. The first request to a DynPage iView calls the doInitialisation method and then the doProcessBeforeOutput method and then calls a doRender method, which sends the entire response to the client. Then, when an event is sent, the doProcessAfterInput is called, followed by the doProcessBeforeOutput method, and then the doRender method is called again, sending the response to the client. This all has to do with the fact that HTTP is stateless, i.e. a request comes in and a response goes out. There is no interrim response sent from the doProcessAfterInput before the doProcessBeforeOutput method is called.
If you could explain the solution in slightly more detail then perhaps I can help. Is this a single iView on the page. Is this iFrame part of the iView itself, in which case the whole iView will ALWAYS be refreshed. This Java class, is it part of the DynPage ?
D
Hi, I have tested the following code in doBeforeOutput(),
String js = "<script> language=\"JavaScript\" alert() </script>";
TextView tv = new TextView(js);
tv.setEncode(false);
getForm().addComponent(tv);
It does not execute the js code.
If I change tv.setEncode(true); I can display the text
<script> language="JavaScript" alert() </script>
It can be executed in html file.
This will work because the script will be executed when the browser reads and interprets the HTML that has been output by the DynPage. Therefore this is not a problem. However, when a HTMLb button is pressed and an event created, you can either call some javascript before the server side event is executed (i.e. using the onClientClick). Once this function has been executed, and you haven't cancelled the htmlb event, the server event is then executed, specified by the onClick method. You can then execute some Javascript once the response has been sent back to the client.
From your original post is sounded like you wanted to be able to execute some Javascript during the server side event, which is not possible.
Does this make sense ?
D
Hi Darrell, thanks for quick reply.
In my case I need server event has to been finished first afterwards js code on client side is executed.
So now the issue is how to send js code from server to client and make this code executed on the client side.
I have tried the code above tv.setEncode(false), but it does not help.
Is something missing ? Thanks for reply.
Kind regards.
Yantong Wang
Hi
The server side event will always finished first as this is how the request-response cycle works. However, it sounds like you have an iView with an iFrame and that you need some Javascript to execute after the iframe has loaded. If this is correct, then you can specify an onload event on the iframe to trigger a javascript function when the iframe has loaded. Therefore on the iframe tag, you would have onload="myevent()". The Javascript for this should either be put into the header of the page (if possible), but if not then it should be put into the page after the iframe creation.
Let me know if this is what you are trying to do, if not, then perhaps attach the code so that I can see exactly what you are trying to do. If you can't attach to this forum message, then please send to darrell.merryweather@sap.com
D
Hi Yantong Wang,
If you need to call server-side 1st followed by client-side... add the needed javascript code into the document/iFrame onLoad() method.
Also, if you really need to insert javascript code into the HTMLB document... you coulf use the class
com.sapportals.htmlb.HTMLFragment
.
Also, add this to your component in the render() method.
However, do remember to escape the javascript(chars like &, ", \ etc).
Regards,
Ashish.
PS: If this helps, don't forget the points
Hello Ashish, thanks for your reply.
Actually the Iframe is already loaded and displayed on the client side. After the onSave() is executed, There is new content should be displayed on the IFrame. That is why I need to refresh the IFrame until the onSave() is finished.
As your suggestion in doProcessBeforeOutput() and I have tried the following code
String js = "<script> language="JavaScript" alert(); </script>";
IPortalComponentRequest request = getComponentRequest();
IPortalComponentResponse response = getComponentResponse();
IPageContext iPageContext = PageContextFactory.createPageContext(request, response);
HTMLFragment htmlFrag = new HTMLFragment(js);
htmlFrag.render(iPageContext);
I could not get an alert displayed and I got a js error when I refresh the browser window. When I comment out code above, there is no error. Can you tell me in details how I can implement sending js code to the client side? Thanks.
Yantong Wang
Ashish
No problems. Find below the code that I sent to Yantong
package com.sap.sdn.test;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.htmlb.*;
import com.sapportals.htmlb.enum.*;
import com.sapportals.htmlb.event.Event;
public class Test extends PageProcessorComponent {
public DynPage getPage() {
return new TestDynPage();
}
public static class TestDynPage extends DynPage {
private final static int INITIAL_STATE = 0;
private final static int SAVE_STATE = 1;
private int state = INITIAL_STATE;
private String name;
/**
Initialization code executed once per user.
*/
public void doInitialization() {
System.out.println("doInitialization");
state = INITIAL_STATE;
}
/**
Input handling code. In general called the first time with the second page request from the user.
*/
public void doProcessAfterInput() throws PageException {
System.out.println("doProcessAfterInput");
InputField myInputField = (InputField) getComponentByName("USER_NAME");
if (myInputField != null) {
this.name = myInputField.getValueAsDataType().toString();
}
}
/**
Create output. Called once per request.
*/
public void doProcessBeforeOutput() throws PageException {
Form myForm = this.getForm(); // get the form from DynPage
Button saveButton = new Button("submit", "Save");
saveButton.setOnClick("onSave");
form.addComponent(saveButton);
if ( state == SAVE_STATE ){
form.addRawText("<script language=\"JavaScript\">alert('Saved');</script>");
}
// create your GUI here....
}
public void onSave(Event evt) throws PageException{
System.out.println("onSave");
state = SAVE_STATE;
}
}
}
User | Count |
---|---|
68 | |
10 | |
8 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.