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”.
Feel free to post a comment for clarification.
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.
<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:
The 2nd parameter ofGetLevel0()(1).DERIVED_JS.HTMLAREA.Value = GetHTMLText(HTML.USERJS, "user_function1()");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.
addLoadEvent() javascript function.Feel free to post a comment for clarification.
i put the exact code but there was no alert flashed, infact i am doubtful that whether the script executed or not ?
ReplyDeletePlease help
the alert will get flashed when you will pass user_function2() in parameter
ReplyDeleteIf &bDisplayAlert Then
&sFunctionName = "user_function2()";
Else
&sFunctionName = "user_function1()";
End-If;
GetLevel0()(1).DERIVED_JS.HTMLAREA.Value = GetHTMLText(HTML.USERJS, &sFunctionName);
Thanks for this code!
ReplyDeleteBut i have different requirement. I want to get the javascript variable value into peoplecode and use it. How can i do this? Since by using "%bind(:1)" we can pass peolecode value to javascript but i need to do opposite. Please help me.
Thanks!
You can try placing one edit box on tge page and find the id of the control and assign the value to thst element in JS. Try ...it should work
DeleteI have put the above code but still did not received any alert. Please advise. I am into fluid mode.
ReplyDeletedoesn't work for me either. Neither of the functions execute.
ReplyDeleteVery useful and exactly what I was looking for from a long time. We had a requirement to mimic few user actions via PeopleCode, I could easily do that by finding out the javascript function names of the user action events and then used your code to execute them. Thanks a lot for this article.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete