473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

usage of javascript variables in asp functions---Urgent help required

m retriving a variable's value which is in javascript into an function in asp,but asp is not able to read dat variable's value,plz direct me for it,its urgent for ma project work.
Feb 6 '08 #1
1 1628
markrawlingson
346 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1. <% if whatever = whatever then %>
  2.   function dosomeJS() {
  3.     alert('this is cool!');
  4.   }
  5. <% else %>
  6.   function dosomeJS() {
  7.     alert('this is so not cool...');
  8.   }
  9. <% end if %>
  10.  
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.

Expand|Select|Wrap|Line Numbers
  1.   if (window.XMLHttpRequest) {
  2.     XMLHttpRequestObject = new XMLHttpRequest();
  3.   } else if (window.ActiveXObject) {
  4.     XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  5.   }
  6.  
  7.    function sendData(dataSource, oField, sSend) {
  8.     if(XMLHttpRequestObject) {
  9.       var obj = document.getElementById(oField);
  10.       XMLHttpRequestObject.open("POST", dataSource, true);
  11.       XMLHttpRequestObject.onreadystatechange = function()
  12.       {
  13.         if (XMLHttpRequestObject.readyState == 4 &&
  14.             XMLHttpRequestObject.status == 200) {
  15.             obj.innerHTML = XMLHttpRequestObject.responseText;
  16.         } else {
  17.             obj.innerHTML = '';
  18.         }
  19.       }
  20.       XMLHttpRequestObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  21.       XMLHttpRequestObject.send(sSend);
  22.     }
  23.   }
  24.  
  25. //invoke the function..
  26.  
  27. sendData('/path/to/your/separate/asp/file.asp', 'anelementsID','yourvariableinfo');
  28.  
  29. //The function takes 3 arguments..
  30. // datasource is the path to the separate asp page i was talking about.
  31. // oField is the ID of an object on your page where you wish to place the outcome (of the function in this case)
  32. // 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.
  33.  
  34. var MyCoolVariable = 'HI!';
  35. var sString = "hello!";
  36. var iInteger = 23;
  37. sendData('/path/to/your/separate/asp/file.asp', 'oMyObject','MyCoolVariable=' + MyCoolVariable);
  38. sendData('/path/to/your/separate/asp/file.asp', 'oMyObject','sString=' + sString + '&iInteger=' + iInteger);
  39.  
  40. //On your seperate ASP Page, you would grab the data as such..
  41. //ASP CODE:
  42. //Request.Form("MyCoolVariable")
  43. //Request.Form("sString")
  44. //Request.Form("iInteger")
  45. //
  46. //More specifically, you could then pass them into your function...
  47. //ASP CODE
  48. //Function Calculate(x,y)
  49. //  Calculate = x*y
  50. //End Function
  51. //
  52. //Response.Write calculate(Request.Form("iInteger"),Request.Form("iInteger"))
  53.  
  54.  
Hope that helps.

Sincerely,
Mark
Feb 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andy Fish | last post by:
Hi, I am in the process of designing a UI which has to be fairly sophisticated. There will be a number of list boxes and other controls, with pop-up windows to edit certain properties. It's the...
2
by: Andy Fish | last post by:
Hi, Can anyone tell me if there is a way to set a variable in javascript and have that persist so that it can be picked up by the next page in the window? In this case I will actually...
3
by: jimmygoogle | last post by:
I posted earlier with a scope problem. I think I resolved it in IE but in Firefox it still exists. Anyone have any ideas/experience with this? I attached my code sorry it is so long. You can...
1
by: david | last post by:
My question is how to keep the values of JavaScript variable unchanged when the page is posted back to the server? Problem: The JavaScript is used in HTML for computing in aspx web page (form)...
2
by: ksr | last post by:
Hello, I have a HTML page, which loads an activeX control in the browser. In the <HEADsection, I have javascript similar to the following, <SCRIPT language="JavaScript"> var Index = ""; //...
1
by: Keithb | last post by:
I have some client-side javascript for that I use for dhtml stuff such as hiding div tags based on user menu selections, etc. I need a way to maintain the state of javascript variables across...
3
by: BAnderton | last post by:
Hello all, Question: Is there any way to access a javascript variable from within psp code? I'm aware of how to do the reverse of this (js_var='<%=psp_var%>'). Here's a non-working...
8
by: Harch84 | last post by:
Hi I have a html page with javascript in it that assigns a set of coordinates to javascript variables. The question I have is how can I then send these variables to a Perl CGI script using a...
1
by: suhas1808 | last post by:
Dear sir, I am suhas patil to require the answer of given question. Can you write a programme in ASP or PHP that reads the JavaScript variables?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.