From time to time, people ask this question, how to pass values from js to asp, and how to pass values from asp to js. In fact, this question is very classic. I have been confused like this before. If you can understand my following sentence well, then these are not problems.
ASP is a server-side language. Its function is to dynamically generate HTML, CSS, JavaScript, etc. that can be recognized by the client browser...
So how does asp pass value to js?
Just generate js dynamically~~ How do we generate html?
Isn't that true?
program code
<table>
<%do while not rs.eof%>
<tr><td>
<a href=<%=rs(id)%>><%=rs(name)%></a>
</td></tr>
<%rs.movenext
loop%>
</table>
How to generate the name value? <%=rs(name)%> So how to generate the value in js?
For example, a js variable js_name needs to get the value of the variable asp_name in asp.
Go and generate
<script>
js_name=<%=asp_name%>
</script>
There is no difference from generating html, the same,
CSS can also generate vbscript, vml, and even xml can be dynamically generated, which is no different from generating html.
If you understand the above, then there is no problem in passing values from asp to js.
Let's talk about passing values from js to asp
How to pass value from html to asp? Two commonly used methods are form submission and the suffix followed by the url to pass the value.
The same is true for js, form submission url suffix
For example
To submit the form, first assign the js variable to an input
<script>
document.form1.inputX.value=js_name
</script>
Then form1.submit(), submit the form
For example
url passes value,
<script>
window.open(www.blueidea.com/index.asp?asp_name=+js_name)
</script>
Again, the principle is the same as html, because js/html are both client-side things.
There is another way to let js-asp transfer values to each other, and that is through cookies
You can set a cookie in asp -> a1=111
To get the cookie value in js, you can use document.cookie in js to get all cookie values.
Then through appropriate interception, we can get the value of a1 to be 111
In the same way, set a1=111 in js, and the correct value of this cookie can also be read in asp.
above:
The same applies to asp/jsp/php/....all server-side languages pass the same value as js/vbs