...

View Full Version : ASP within Javascript function



bostjank
01-27-2003, 03:23 PM
Hi!

Is it possible to execute ASP statement within JavaScript function. I have tried to do so, but those ASP statements are executed even if Javascript function is not called.

This is my example:


<SCRIPT language=JavaScript>
function pexit() {
alert('You're leaving...');
<% Session("CheckExit") = "jupi" %>
}
</SCRIPT>

<BODY onUnload="pexit()">

<% Response.Write Session("CheckExit") %>


Thanks,
Bostjan

allida77
01-27-2003, 05:54 PM
You can not call server side functions from the client unless you are submitting a form or refreshing the page. I am not sure what you are trying to write but try

function pexit() {
alert('You're leaving...');
varFoo = <% Session("CheckExit") = "jupi" %>
}

now the client side variable "varFoo" will hold the value of the server side variable when sent to the client.

bostjank
01-27-2003, 06:42 PM
Actually it was a bad example from my side - what I would like to do is to write to database when the JavaScript function is called. And because I call the function when the page is unloaded, I cannot use ASP.


<SCRIPT language=JavaScript>
function pexit() {
alert('You're leaving...');
<% WRITE TO DATABASE %>
}
</SCRIPT>

<BODY onUnload="pexit()">

Do you have any other idea on executing ASP statements on page unload?

Bostjan

BigDaddy
01-27-2003, 07:27 PM
I would think you'd want to use the javascript to submit a form, hitting the server and writing to the database.

whammy
01-28-2003, 12:32 AM
Me too. ;)

bostjank
01-28-2003, 04:08 PM
I cannot use the submit event, because I would like to write to database, when a visitor EXITS the page - that is on Unload event (and this cannot be done just with ASP).

There is another option (or so I believe) - calling the ASP page from javascript. Such Javascript functions are often inserted when somebody uses third party statistics apps - do you know anything about this?

Bostjan

whammy
01-29-2003, 02:08 AM
One easy way to do it is to use a fake image, which isn't really an image (this is sometimes called a "tricky pixel"):

<img src="something.asp?myvariable=<% = myvariable %>" width="1" height="1" />

That's basically a cheesy (but workable) way to do remote scripting in some cases...

I'm not sure how reliable this would be onunload, though - perhaps if you called a function onunload that first changed the image's source to the asp page, and used setTimeout() before closing/redirecting the page it might work - but I haven't tested it...

wyattwebb
03-13-2003, 10:19 PM
On the onload function open a small popup window that contains the neccesary ASP to write to the Database and use Javascript to close that window within a couple of seconds.

david7777
03-14-2003, 08:16 AM
Originally posted by wyattwebb
On the onload function open a small popup window that contains the neccesary ASP to write to the Database and use Javascript to close that window within a couple of seconds.

That isn't the best option though - the user might close the window before the database is updated, or there could be a timeout and no notifictaion of update failure, or or or...

I tried a similar thing once, but landed up changing the page to use ASP, and abandoned the on exit stuff...

BigDaddy
03-14-2003, 02:09 PM
I use Crystal Reports on the web. When a report is run, it produces a small, 1x1 inch window called "cleanup.asp". It does just what it says...it cleans up, clears the session id's out, etc.

You could also do something like that, but maybe put a graphic or a bit of text in it stating something like

"Thank you, now processing your request..."

That would intuitively tell the user that if they close it in the 2 seconds it takes to do it, that they might be interrupting something they shouldn't.

glenngv
03-15-2003, 05:49 AM
extending whammy's approach...


<script language="javascript">
function pexit() {
//write to db
var db = new Image();
db.src = "writeToDB.asp";//asp page that writes to db, can pass query string
alert("You're leaving...");
}
</script>

<body onUnload="pexit()">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum