Problem to show JSP variables in javascript alert ()

1

I am testing warning messages on my website, I am collecting values passed through the session and showing them in an alert.

In this first part I get the values, check it and pick it up perfectly

<%
String registro= (String)session.getAttribute("registro");
String permisos= (String)session.getAttribute("permisos"); 
Integer estado= (Integer)session.getAttribute("estado");
String nombre= (String)session.getAttribute("nombre");
String clave=(String)session.getAttribute("clave");
%>

Now with this code I have the message displayed as soon as the page is loaded

<body class="hold-transition skin-black sidebar-mini" style="height: auto; 
min-height: 100%; overflow-y:auto; " onload="permisosMenu()">

Here the javascript code is quite simple

<script >
//esto es JavaScript 
  function permisosMenu() {
      var cadena = <%=permisos%>;
  alert(cadena);
}
</script>

And when I run it shows me this message (which is correct)

THE PROBLEM

When I try to pass another value that is not that variable, such as the "registry"

<script >
//esto es JavaScript 
  function permisosMenu() {
      var cadena = <%=registro%>;
  alert(cadena);
}
</script>

I get the following error by console (Be careful if you get the value of the registry that would be "admin", but do not boot it by the alert)

Does anyone know what my mistake is?

    
asked by angelo1793 15.08.2018 в 19:18
source

2 answers

1

What surprises me is that it does not give you an error with permissions.

The error that you generate is due to the basic behavior of javascript, here are some examples:

 var dato = 2; 

No variable can be named with a number at the beginning, therefore 2 is assigned as value number of variable dato

var dato = "Lorem Ipsum";

It is in quotes so that is pure text the value of dato is simply what is in those quotes

var dato =  algo;

It does not go in quotes, therefore it is not text, it does not start with a number? it is not a numeric value then, therefore it is another variable or object

Explanation
The problem in your case is due to this last intepretation,  At the time of loading the page the code remains such that:

var cadena = admin;

Therefore at the time of declaring the variable cadena first try to find out which variable or object is admin , but as indicated by the console; admin does not exist.

Solution:
To be interpreted as pure JavaScript text you simply have to write it in inverted commas:

var cadena ="\"" +  <%=registro%> + "\"";

O

var cadena ='"' +  <%=registro%> + '"';
    
answered by 15.08.2018 в 20:25
0

The solution as I commented to the user srJJ, is to put the quotes between the variables to convert it into string

<script >
//esto es JavaScript 
  function permisosMenu() {
      var cadena = '<%=registro%>';
  alert(cadena);
}
</script>

On the other hand, very apart from being solved the problem, it follows the unknown that the variable "permissions" does not need the quotes to be recognized.

    
answered by 16.08.2018 в 02:19