Method of triggering JavaScript when JSFF or JSF page loads

Source: Internet
Author: User

Phenomenon One

Recently encountered in a project, some page elements are dynamically rendered through JavaScript when the page is loaded. When JavaScript scripts that generate these elements are placed in the jspx file, there is no problem with rendering the interface. But when we put the JS script that generates these page elements into the JSFF, we find that the JS script is executed only when we enter the first view of Taskflow, and then the JS code of the subsequent view is loaded and executed when we enter the subsequent view.

AnalysisThrough analysis, it is found that when entering the first view of Taskflow, the JS code introduced by the <af:resource/> tag in the first view can be added to the page successfully, and the OnLoad event of the page is executed, so the page element renders successfully. However, when moving from the first view to a subsequent view, the JS code introduced by the <af:resource/> tag in the subsequent view is not added to the page, so the page elements cannot be rendered. But if we refresh the page at this point, the elements on the page will be displayed correctly. This is because the ADF regenerates the HTML code of the page after the refresh, re-parses the <af:resource/> tag in the view, and introduces the relevant code into the page. SolveIn the <af:resource/> label coat with <af:panelgrouplayout/&gt, so that the resource will be included in the page every time. Phenomenon TwoSolves the previous problem, now that all view in Taskflow is present, the JavaScript code is correctly introduced into the page and executed to correctly generate the page elements. But soon I found another problem, that JavaScript code could only be called successfully the first time the view was displayed. If we flow from one view to another and then back again, the OnLoad event's JavaScript will not be executed. AnalysisWhen a view is rendered by the ADF, the view is cached and will not be rendered again when it is accessed again and triggers the onload event. I tried to set the refresh configuration of the Taskflow, but it was fruitless. The point is that there is no guarantee that the OnLoad event can be triggered every time the view is entered, and since client script is not working, is it possible to activate the appropriate JavaScript code for element rendering on the server side as the page loads? A Java method that triggers the background when the page is loaded, and then triggers the JS method within the Java method (about how to invoke JS in Java, refer to my this article)。 Following this idea, we can do this by adding executable to the page. Through the search I found that there are people on the internet who have similar problems and solve them through this method. However, the disadvantage of executable is that it is necessary to generate a custom Java Bean Data control and be more cumbersome to configure (you need to configure method binding and executable for each page). SolveAfter a constant attempt, I finally thought of a simplified method: 1. Add a <af:outputText/> tag to the view; 2. Binds the value of <af:outputText/> to a bean's property #{mybean.dummy};3. In fact, there is no need to define the dummy attribute in Mybean, just define a getdummy () method and add the logic to invoke the JS code, and the ADF will re-execute all El expressions, including #{mybean.dummy}, whenever JSFF unfolds. The logic in Getdummy () will be executed, and the corresponding JS method will be called. So our needs are fulfilled. However, I think it might be more rigid to complete the invocation logic of JS entirely in the bean. If we need to change the logic of invoking JS, we have to modify the bean's method. Finally I will invoke the logic of JS to modify, so that it invokes the script content to point to the foreground <af:outputText/> tag's Shortdesc property: private Richoutputtext scriptsetting;//outputtext's binding variable   Public String Getdummy () { String script = Scriptsetting = = null? "": Scriptsetting.getshortdesc (); Get script Content invokejavascript (script);//Call JS return "";     }If a page needs to invoke the JS script at load time, only the following 3 steps are required: 1. Register the mybean;2 in the Taskflow. Place the following code in the page; <af:outputtext shortdesc= "youjsfunction (); value= "#{mybean.dummy}" id= "ScriptControl" binding= "#{mybean.scriptsetting}"/>3. Modify the Shortdesc attribute value to the desired JS code. The source code for the sample is Download Here, the JDeveloper version used is 11.1.1.6.0. As long as the thought does not slide, the method is always more than the problem: Yuan Fang, what do you think? Reprinted from: http://blog.sina.com.cn/s/blog_671b3b1001018vr3.html

Method of triggering JavaScript when JSFF or JSF page loads

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.