Wed 30 Nov 2005
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)
<input type="hidden" name="USERJSINJECTION" value=""/> <script type="text/javascript"> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } } function user_function1() { window.print(); } function user_function2() { alert('Hello from user javascript'); } addLoadEvent(function() { %bind(:1) }); </script> - 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
<input>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.
Feel free to post a comment for clarification.
November 16th, 2006 at 8:03 pm
Super tip..
kepp this site live n helping other techies :-)
February 22nd, 2007 at 7:15 am
I’m just wondering if you ever had further musings on the “Modifiable by JavaScript” property.
July 27th, 2007 at 5:10 am
Great info…really helped me out.
February 21st, 2008 at 10:19 pm
I used it to execute Javascript that takes user back to Portal Homepage on click of a button on a MessageYesNo. This is amasing piece of code, helped me a lot! Thanks.
Taras
August 29th, 2008 at 2:29 pm
Has anyone tried this on PT8.49? I can’t seem to get it to work, and I followed the instructions to the letter. The JavaScript just isn’t inserted. (If i right-click on the page, and do a View Source, I can’t find any reference to the USERJSINJECTION or my function - yet I’m sure the code has fired.)
Thanks!