In short, You can't do this. Well, not directly.
ASP is a SERVER side language, javascript is a CLIENT side language. As such, variables declared within javascript are declared and used by the client (browser) - they cannot be directly read, changed, manipluated, or otherwise by a function within your server side ASP code. You can manipulate javascript using ASP, but not the other way around
EG:
-
<% if whatever = whatever then %>
-
function dosomeJS() {
-
alert('this is cool!');
-
}
-
<% else %>
-
function dosomeJS() {
-
alert('this is so not cool...');
-
}
-
<% end if %>
-
However, there is a solution. If you use AJAX, you can send the value of your javascript variable through a backend form get/post method to another ASP page, then grab the data using the request object and return the data for further manipulation back to your current page.
In your case, if you're wanting to send the JS value to an ASP function you would create a separate ASP page in the backend which would pick up the value of the JS variable, pass it into the function, and return the function's result back to the current page.
-
if (window.XMLHttpRequest) {
-
XMLHttpRequestObject = new XMLHttpRequest();
-
} else if (window.ActiveXObject) {
-
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
-
function sendData(dataSource, oField, sSend) {
-
if(XMLHttpRequestObject) {
-
var obj = document.getElementById(oField);
-
XMLHttpRequestObject.open("POST", dataSource, true);
-
XMLHttpRequestObject.onreadystatechange = function()
-
{
-
if (XMLHttpRequestObject.readyState == 4 &&
-
XMLHttpRequestObject.status == 200) {
-
obj.innerHTML = XMLHttpRequestObject.responseText;
-
} else {
-
obj.innerHTML = '';
-
}
-
}
-
XMLHttpRequestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
-
XMLHttpRequestObject.send(sSend);
-
}
-
}
-
-
//invoke the function..
-
-
sendData('/path/to/your/separate/asp/file.asp', 'anelementsID','yourvariableinfo');
-
-
//The function takes 3 arguments..
-
// datasource is the path to the separate asp page i was talking about.
-
// oField is the ID of an object on your page where you wish to place the outcome (of the function in this case)
-
// sSend is a string of information to send to the back-end ASP page. it can be a variable in JS, or some other information. Examples below.
-
-
var MyCoolVariable = 'HI!';
-
var sString = "hello!";
-
var iInteger = 23;
-
sendData('/path/to/your/separate/asp/file.asp', 'oMyObject','MyCoolVariable=' + MyCoolVariable);
-
sendData('/path/to/your/separate/asp/file.asp', 'oMyObject','sString=' + sString + '&iInteger=' + iInteger);
-
-
//On your seperate ASP Page, you would grab the data as such..
-
//ASP CODE:
-
//Request.Form("MyCoolVariable")
-
//Request.Form("sString")
-
//Request.Form("iInteger")
-
//
-
//More specifically, you could then pass them into your function...
-
//ASP CODE
-
//Function Calculate(x,y)
-
// Calculate = x*y
-
//End Function
-
//
-
//Response.Write calculate(Request.Form("iInteger"),Request.Form("iInteger"))
-
-
Hope that helps.
Sincerely,
Mark