Re: Calling PL/SQL function from JavaScript

From: Vladimir M. Zakharychev <bob_at_dpsp-yes.com>
Date: Thu, 6 Sep 2001 16:47:51 +0400
Message-ID: <9n7r2h$31g$1_at_babylon.agtel.net>


To follow on John Russell's reply, here's a little trick with hidden IFRAMEs that we were using in our projects for quite some time (regretably, it requires that browser is Internet Explorer 5.x or later as Netscape and Opera seem to have incomplete implementations of both IFRAMEs and CSS1 - correct me if I'm wrong). The idea behind the trick is quite simple: IFRAMEs are just embedded frames, with the same capabilities, but one good feature that we use here: it's display may be set to none (style="display:none") and it will become invisible. Second part of the trick involves the way JavaScript/JScript accesses document in a different frame. You probably know that you don't have direct access to any properties in a cross-frame document. But what you can do is call any method in that document. We are using this fact to dynamically change content of the document without refreshing it with new parameters that may alter the content. Here's simple example:

  1. main document defines an IFRAME and a callback function:

<script language="JavaScript">
function callbackFunction(param)
{

 // do whatever you want here to alter the document according to 
 // passed parameter, for example, use document.createElement('OPTION')
 // to create a new option value and add it to a select. 
}
</script>
<iframe name="iframe1" src="initial.src" style="display:none"></iframe> ...
<!-- now some code to get the new data on event here I assume that you are calling pl/sql code through mod_plsql or OWA or a servlet.
-->

<button onClick="java script:document.frames('iframe1').src='some.plsql.procedure?param=value';return true;">Get New Data</button> ....

when user clicks Get New Data button, this triggers execution of some.plsql.procedure in context of the hidden IFRAME. How do we get the results from that IFRAME to our main document? That's what callbackFunction() is for.

some.plsql.procedure(param varchar2) is begin
htp.p('
<script language="JavaScript">
 parent.callbackFunction("whatever value you want to pass"); </script>
');
end;

That's it! Being executed in context of the IFRAME, this JavaScript just calls parent.callbackFunction() (which is defined and accessible, unlike properties of parent, like innerHTML or anything else) and passes some dynamically generated data to it. callbackFunction() then alters content of the parent document, because it is defined in context of that document and has access to all its properties and objects! For example, this function may repopulate some SELECT with new OPTIONs, or do anything else with the data it receive.

Hope this trick would be of help to you as it was to us.

regards,
Bob. Received on Thu Sep 06 2001 - 14:47:51 CEST

Original text of this message