You pass data to web applications through either the query string or as part of an HTTP post request within the body. As Javascript is only executed after the page has been sent, the only sending a value back is to make another request to the server containing the data.
This can be achieved using a hidden frame or iFrame and setting form values within the page and submitting it. A better way to do it however is through the use of the XMLHTTPRequest object, available in both Firefox and Javascript and in Opera as far as I am aware. This technology more commonly known as AJAX, is used to update data on the page without the user having to make a request by submitting a form or clicking a link.
Before you consider using this, what is it you are trying to do?
Is the value of the variable likely to change? If not then there is no need to insert its value into the page.
There are many better ways of doing this. Don't use classic ASP for a start As i said in my previous post, if you need to fetch and set variable values on the server, you should look into AJAX. But I can't really see what you are using the Javascript for.
You are not understanding the way in which ASP works. Javascript has nothing to do with ASP. You cannot assign a value to a server side variable from a client side script (not in the way you are trying to do it).
Treat each as a separate program running on a separate PC, because that is in effect what they are. In order to communicate, you either need the user to initiate a request by clicking a link or submitting a form or have the Javascript initiate a request independently (AJAX).
The output of your ASP script (i.e: the executed VBScript) is the HTML page and the Javascript code. So once executed, the ASP will produce this Javascript code:
Code:
var MapProvider = 'value from asp';
When executed the value from your ASP script is then assumed by the client side Javascript. The ASP script has long since executed and no longer exists, the memory in which the variable was stored has been released and is probably being used to store someone elses map provider.
Think about the Javascript the following line would produce:
Code:
<%= session("MapProvider") %> = MapProvider ;
Once the VBScript has been executed you get:
Code:
value of map provider = MapProvider ;
This would produce an error in your Javascript and demonstrates what I stated earlier, that you cannot set server side variable from a client side script.
This is the typical life cycle of an HTTP request to a ASP script:
User makes a rquest for your page through their web browser.
The HTTP Request is sent through the Internet and is received by the server hosting the script.
The web server software (probably IIS) dispatches the request to the ASP engine.
The ASP engine finds the script and executes it. The output of the script is sent back to the web server software.
The script output is then taken and sent back to the client as a HTTP response.
The users web browser receives the response. This could be a HTML page, Javascript, an image or any other kind of file.
The browser takes the appropriate action to render the output. Should the output be HTML it is rendered in the browser. If it contains any Javascript, the Javascript is then executed.
By the time the output of the ASP script has been sent to your browser and it has started executing your Javascript, the runtime execution context of the script has long since been disposed of. It is for this reason that you cannot set variables inside your ASP script from a Javascript.
OK - I understand that. Since $400 for Ajax is a non starter it looks like I'll have to stick to my "messy" code and "dump" the variable on the web form for retrieval by the vb form code. My problem was getting a field(variable) on the form that BOTH VB and Javascript could access. My "bad practice" of writing html from VB is the only way I know of doing it.
OK - I understand that. Since $400 for Ajax is a non starter it looks like I'll have to stick to my "messy" code and "dump" the variable on the web form for retrieval by the vb form code. My problem was getting a field(variable) on the form that BOTH VB and Javascript could access. My "bad practice" of writing html from VB is the only way I know of doing it.
If someone knows a better way ...........
AJAX is Asynchronous JavaScript and XML, it is free. AJAX is just a technique used by web developers to use javascript to dynamically change their web page, usually with a partial refresh, or none at all.
It is FREE. What ever you see for $400 is probably a library someone is charging for. So to solve your specific problem, the general technique to pass variables for asp/php to javascript is to generate the javascript with asp/php and that way you can pass the variables that way. Other than that AJAX is your only option, probably through a webservice, because you won't need to refresh you page if go that route.
You can get a free AJAX library if you do like 10 seconds of research on google to do this.
Re: [RESOLVED] Javascript - pass variable to VB.net(2003)
Mate mate mate. Go back and read post 13 carefully again.
AJAX is not a product! It's a design technique. You can implement the server-side of AJAX on any platform under the sun, as long as it supports HTTP. It is just a "buzzword" name for using the XMLHttpRequest object in JavaScript to asynchronously retrieve data from the server in XML. That is all it is.
As for topic: if you are wondering how to pass variables between the client-side and the server-side, you're thinking about it the wrong way. The server-side and client-side are two completely separate programs on completely separate machines! I cannot emphasise this enough. Don't generate client-side script dynamically; write the two individually. The only thing either should know about the other is the client-side's knowledge of the server-side's API.
Re: [RESOLVED] Javascript - pass variable to VB.net(2003)
That's how you should do it in this case. Get your javascript to set the value of a hidden field, but make sure the hidden field has a runat="server" attribute in the ASP.NET design view so that you can access it in codebehind.
Yes. I wish Microsoft hadn't called their AJAX framework AJAX.NET. It does rather confuse things.
It's irritated me ever since the release. It seemed to me something motivated by search engine greed. Searching for AJAX definitely obscures relevant results.
Re: [RESOLVED] Javascript - pass variable to VB.net(2003)
according to this code, how can i trigger a function in the code behind file when the hidden value changes in the script???
i tried the hidden events but it didn't work & i couldn't know wht's wrong with it
The only solution is to assign value to a control and then read it using js.