简体   繁体   中英

How to call asp function on onclick event?

I want to call one asp function on onclick event.

<img src="..." style="cursor: pointer;" onclick="..." />

Here at onclick I want to call following function:

function getproject(valuenew)
    response.Cookies("projectname") = valuenew
end function

How should I do this?

It does not make sense to return to the server to set a cookie. You can set a cookie very easily using JavaScript. Here is an example:

var expire = new Date();
expire.setTime(today.getTime() + 3600000*24*5); // expire in 5 days

document.cookie="projectname=valuenew;expires=" +expire.toGMTString();

Here is a link with more info: http://www.w3schools.com/js/js_cookies.asp

You don't have to use ASP to set cookie, but I'll give you simple way to execute ASP code after clicking element.

First, add hidden frame to your page:

<iframe id="ajaxFrame" src="about:blank" style="display: none;"></iframe>

Now in the onclick have this:

onclick="document.getElementById('ajaxFrame').src = 'PageNameHere.asp?cookievalue=Your_New_Value_Here';"

And the last step, add this to your ASP code: (on top)

If Request.QueryString("cookievalue")<>"" Then
    getproject(Request.QueryString("cookievalue"))
    Response.END
End If

This is not the best practice, but as you're a beginner it's the most simple way I know of.. when you gain some experience look into real AJAX, most simple and powerful is jQuery AJAX .

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