Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    The sunny side of the Alps
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ASP within Javascript function

    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:
    Code:
    <SCRIPT language=JavaScript>
    function pexit() {
      alert('You're leaving...');
      <% Session("CheckExit") = "jupi" %>
    }
    </SCRIPT>
    
    <BODY onUnload="pexit()"> 
    
    <% Response.Write Session("CheckExit") %>
    Thanks,
    Bostjan

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Cincinnati, OH
    Posts
    545
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    The sunny side of the Alps
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    <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

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would think you'd want to use the javascript to submit a form, hitting the server and writing to the database.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Me too.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    The sunny side of the Alps
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #8
    New Coder
    Join Date
    Dec 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Feb 2003
    Location
    South Africa
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #10
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    This is in spanish when you're not looking.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    extending whammy's approach...
    Code:
    <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()">


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •