It is possible to assign the value of a Javascript variable to an ASP

Asked

Viewed 3,586 times

0

I wonder if it is possible to assign a variable in Javascript for ASP?

Example:

Dim ASP
var JS;

ASP = 1
JS = 2;

ASP = JS

Response.Write ASP ' saida deveria ser 2 e
  • It depends on how you are using Javascript because there is Javascript server-side, but I don’t think that’s your case. To send Javascript data to a server-side script, use xmlhttp (ajax) or default http..

  • ok I will search on, thank you

  • An identical question, but with PHP... http://answall.com/questions/25136/igualar-vari%C3%A1vel-php-a-Vari%C3%A1vel-javascript/25275

3 answers

5

Javascript is processed by the Browser and ASP is processed on the server before delivering to the user.

Under these conditions, the answer is No, it’s not possible.

You would have to use Javascript to send the information by POST or GET to an ASP page, and so you capture the value by request and stores in the ASP variable.

The Reverse (Storing value of an ASP variable in a JS variable) is possible by doing only:

<script type="text/javascript">
    var x = <%=y%>;
</script>
  • 2

    Be very careful if you want to pass an ASP string to JS that way. It is a lot to write the program in a way that is vulnerable to code injection attacks (XSS)

  • @hugomg I found your comment about XSS interesting. Would you have articles in Portuguese to share? I’d like to delve into it. Thank you.

  • Usually I point people to that article. It is the most complete I know on the internet but is in English... As for my comment, the problem is similar to SQL injection. If you do var x = "<%=y%>", a y value containing quotes such as oi";boom();" will allow user input JS code on your page.

0

If you are doing javascript for you to call the variable in ASP you should do as follows:

'Bloco ASP    
Dim ASP
ASP = 1

'Bloco Javascript
<script>
   var JS;
   JS = 2;
'<%=ASP%>' = JS;    
</script>

0

Try passing the value of the JS variable by Innerhtml to an Hidden field and then recover in ASP by Request.

Here’s an example of what it would be like:

'Mandando o valor da variavel por JS:
document.getElementById("variavel").innerHTML = "Valor da Variavel JS";

'Campo que receberá o valor do JS.
<input type="hidden" id=variavel name=variavel> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.