简体   繁体   中英

Execute classic ASP code on mouse over

I would like to execute this Classic ASP code when passing the mouse over a div.

<% 
  dim s
  s=Request.QueryString("undercity") 
  Response.Write(s)
  set fs=Server.CreateObject("Scripting.FileSystemObject")
  if s<>"" then
    set f=fs.OpenTextFile(Server.MapPath("RDS/rds.txt"),2,true) 
    f.WriteLine(s)
    f.Close
    Set f=Nothing
  else
    if (fs.FileExists(Server.MapPath("RDS/rds.txt")))=true then 
      set f=fs.OpenTextFile(Server.MapPath("RDS/rds.txt"),1) 
      Response.Write("On air: "+f.ReadAll)
      f.Close
      Set f=Nothing 
    end if
  end if
  set fs=Nothing 
%>

I tried creating a function on my js file using the .innerHTML="<% code %>" but it didn't work.

How should this be done?

You can't really do that that way. The key is the "passing mouse" event occurs on the client machine. Where ASP code runs on the server machine. So you have to have some way to hook them together.

They way that they appear to work together is, When user request an asp page with the browser on their machine, their browser sends a request to your web server, your web server runs your asp code, which generates HTML code. It then send the result HTML code to the client, the client browser receives the result HTML code and then takes over from there. After this user can move the mouse to trigger some JavaScript code, but this is long after your ASP code has run and it happens in the client computer.

In order for a client event to "call" server side code, you would have to trigger some kind of server request. A "form.submit" a typical example where client JavaScript code can call form.submit function to submit the form, which causes the browser to post back the page to the server, then your server code get called again, then it generates some new HTML and send back to the client browser, then the client browser takes over, over and over like this.

You can use AJAX to "call" back to the server any time in your page. I am familiar with ASP.NET AJAX but I do not know what you can use with ASP. However the concept is the same, which is you have to somehow go back to the server side in order to run server side code.

Hope this helps a little bit.

The fastest way would be to create mini-form that point to your ASP file as an action, eg

<form id="myForm" action="mycode.asp?undercity=somedata">
    <div onmouseover="document.getElementById('myForm').submit()" >
        Mouse over me!
    </div>
</form>

The above code defines a form "myForm" and DIV with mouseover function that submits the form. You can add required query string parameters to the page specified in Action.

Basic demo: http://jsfiddle.net/S5GFN/3/

NOTE: This action will reload entire page. If you need to replace .innerHTML of an element - you will have to perform an AJAX call. I recommend using jQuery's .load() method, using it you can do a simple call in client-side code eg:

$("#mouseOverDiv").mouseover(function() {
   $("#myElement").load("mycode.asp?undercity=somedata")
});

Here's an example: http://jsfiddle.net/S5GFN/5/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM