Previously, I’ve presented a technique on how to effectively append javascript execution in your PeopleSoft pages. The technique could be used to append a stylesheet to a PS page; or run code to beautify the page, perhaps adding fancy functionality like drag-and-drop to the elements. Javascript execution using the technique, however, is static. It applies to those scenarios where you want the custom javascript to run at every page load.
This how-to shows how to run javascript on your PeopleSoft pages conditionally. Here, PeopleCode sets the logic that determines when the javascript code will run. As noted in a previous post, this is not as simple as dropping a HTML Area on your page and setting the script in PeopleCode. This is because the value in the HTML Area field remains and the javascript code will keep executing at subsequent page refreshes. The solution though is not difficult, only a couple of additional simple steps are necessary.
The following steps assumes that you have derived/work field named DERIVED_JS.HTMLAREA which you will be using for this “PeopleCode-javascript pseudo integration”.
Create a HTML definition as your javascript template. Include all the necessary user-defined javascript functions that you need. (For the purpose of this how-to, let’s assume you save this HTML definition as USERJS)
At the scroll level 0 of your PS page, insert a HTML Area control. Assign this to the DERIVED_JS.HTMLAREA field.
Again at scroll level 0 of your PS page, insert an editbox control. Assign this again to the DERIVED_JS.HTMLAREA field. Set the following page field properties:
On the Use tab, check Invisible and Modifiable by JavaScript (in earlier versions of PeopleTools, this may be labeled as Modifiable from HTML)
On the General tab, set Page Field Name to USERJSINJECTION.
This is a critical step to prevent repeated execution of your user javascript. This control, in conjunction with the element added in the HTML definition, is a convenient mechanism to clear the value of DERIVED_JS.HTMLAREA on the next server-side execution (be it a prompt-table lookup, or PeopleCode execution like FieldChange or save processing). I will explain the Modifiable by JavaScript property in more detail in a future post.
Now in PeopleCode, to execute your javascript function, all you have to do is populate DERIVED_JS.HTMLAREA with the HTML definitions contents:
GetLevel0()(1).DERIVED_JS.HTMLAREA.Value = GetHTMLText(HTML.USERJS, "user_function1()");
The 2nd parameter of GetHTMLText() substitutes the value of %bind(:1). It effectively specifies which javascript function to execute. Make sure that the parenthesis (along with function arguments, if any) is always included.
That’s all there is to it. Note that the javascript is actually executed immediately after the page refresh that occurs after PeopleCode executes. The related posts provides explanation of the addLoadEvent() javascript function.
No comments:
Post a Comment