Saturday, 30 May 2009

Execute Javascript through PL/SQL

Sometimes, like in this case, where I needed to set the focus on load to a specific line in a table when inserting new rows, you need to execute javascript within your PL/SQL block. In this example in my Demo Application you will find an explanation of the code you need for that.


John Scott said...

Hi Denes,

Just to be pedantic, the Javascript is not actually being executed by the PL/SQL, the PL/SQL is purely emitting the Javascript code (essentially text at this point) to the browser. The browser then interprets this code during the page rendering phase (rendering in the browser I mean).

It might seem like a subtle difference, but I just wanted to point out that the Javascript is not running at the same time the PL/SQL code is running in this case.


Denes Kubicek said...

Sure, but this is how this question will be asked and understood by the most. This is why I picked that title. Whoever reads the code will soon notice that there is no execution of the javascript through PL/SQL. PL/SQL is used to put the javascript code together and send it to the browser.

Chris said...

By the way, it's not that hard to really "execute javascript through PL/SQL". About a year ago, I loaded rhino into a server and played with it a little bit. It never had any use except to prove to myself that I'm still a nerd ;-).

Anonymous said...

Hello Denes,
I understood what u have specified, but I want to do some other thing, I want to run a sql query while checking a condition in javascript like this
htp.p('if(v == true){');
update aggrement set checked='y' where agg_no = :P2_AGG_NO;
update aggrement set checked='n' where agg_no = :P2_AGG_NO;
but it is not working. This always insert 'n' in "checked" column.
Please Help me out on this.
you can send me a mail on this on

Denes Kubicek said...

I don't think you need javascript for that since this can be done in the query itself. See this example from Patrick Wolf:

Denes Kubicek

Anonymous said...

actually i m doing this in a query only see this whole logic

Cursor agg is
select Agg_no,client.Client_name clientn,End_date from AGGREMENT,client WHERE client.client_id = aggrement.client_id;
for agrmt in agg loop
:P2_END_DATE := agrmt.End_date;
:P2_AGG_NO := agrmt.Agg_no;
:P2_CLIENT_NAME := agrmt.clientn;
if to_date(agrmt.End_date,'dd-mon-rr') <= to_date(SYSDATE,'dd-mon-rr') THEN
htp.p('open script);
|| :P2_AGG_NO
|| ''','''
|| ''','''
|| ''');');
htp.p('if(v == true){');
update aggrement set checked='y' where agg_no = :P2_AGG_NO;
update aggrement set checked='n' where agg_no = :P2_AGG_NO;
htp.p('}close script');
end if;
end loop;

This is my whole logic where I am calling a javascript function from select query from that function I m getting the value of 'v' which is a boolean variable and after checking this variable's value I want to update my table accordingly.

this is the Pop function:

var v;
function pop(ag_no,end_dt,clnt_nm)
v = confirm("Aggrement: " + ag_no + " with Client: " + clnt_nm + " is going to end on " + end_dt + " Date.");

For this either I need to run the update table qurery under javascript or I can reffer the javascript variable 'v' into my pl/sql function.

Anonymous said...

Sir please reply soon, Because I need it in my project on which I am working.

Waiting for your reply..

Denes Kubicek said...

I am sorry but I still do not understand your requirement. I see your statement but I don't see what exactly you want to do with it.

Denes Kubicek

Anonymous said...

Actually I have a Confirm Box,
which will get popped up when the aggrement end date is less than or equal to today's date, through that confirm box I am getting the value in variable "v" as true or false(after user pressed OK or CANCEL). After checking this value I want to update my "Aggrement" table.

So my problem is that the variable "v" is a Javascript variable and I want to update my table after checking this variable's value.

If u can tell me how can I access the variable "v" in PL/SQL function? If I can't do this than tell me can I run my update query in javascript after checking the value of "v".

Hope this explanation helps u understand my problem.

Thanks for helping me out. Please see if u can reply soon.

Anonymous said...

If you want you can check this in my workspace

Login Information:

Application: Sales Management-2098
Page:Aggrement(page no.2)

Anonymous said...

Sorry I forgot to mention
You can login on using above login information

Denes Kubicek said...

I will have a look at it when I have time. I am currently bussy with my daily stuff.

Denes Kubicek

Nipun said...

hi denes,

how it possible to show a "user choice confirmation message" during a pl/sql process..?

Arnaud said...


I have the same question that Nipun

I try to execute sql after a javascript confirm

htp.p('if (confirm("Do you want to delete your record?")) {' );
delete emp where emp_id = :P1_EMP_ID; );

But the delete will be execute at each time.

Arnaud said...

I find something like that

If you cancel return to your page else delete the row

htp.p('if (!confirm("Do you want to delete your record?")) {' );

htp.p('alert("delete in progress : ");' );

delete emp where emp_id = :P1_EMP_ID;

Nandkishor Wagh said...

interesting piece of information, I had come to know about your web-page from my friend pramod, jaipur,i have read atleast eight posts of yours by now, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a million once again, Regards, sql and plsql difference

YouLoseBellyFat said...

script language examples on javascript code examples javascript source

for IT the said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai