Call file js containing variable java, from html5

0

I have a JS function embedded in my website, at the end of body

<script>
function permisosMenu() {
  var cadena = <%=permisos%>
  if(!cadena.includes(1) && !cadena.includes(2) && !cadena.includes(3) && !cadena.includes(4))
        document.getElementById("bloque1").style.display ="none";

  if(!cadena.includes(5) && !cadena.includes(6) && !cadena.includes(7) && !cadena.includes(8) &&
          !cadena.includes(9) && !cadena.includes(10) && !cadena.includes(11) && !cadena.includes(12) &&
          !cadena.includes(13) && !cadena.includes(14) && !cadena.includes(15) && !cadena.includes(16))
        document.getElementById("bloque2").style.display ="none";

  if(!cadena.includes(6) && !cadena.includes(7) && !cadena.includes(8) && !cadena.includes(9) 
          && !cadena.includes(10) && !cadena.includes(11) && !cadena.includes(12))
        document.getElementById("bloque2_op2").style.display ="none";

  if(!cadena.includes(17) && !cadena.includes(18) && !cadena.includes(19) && !cadena.includes(20) )
        document.getElementById("bloque3").style.display ="none";

  if(!cadena.includes(21) && !cadena.includes(22) && !cadena.includes(23))
        document.getElementById("bloque4").style.display ="none";

  if(!cadena.includes(24) && !cadena.includes(25))
        document.getElementById("bloque5").style.display ="none";

  for (var i = 1; i <= 32; i++) {
      if(!cadena.includes(i)){
        document.getElementById("op"+i).style.display ="none";
    }
}
}
</script>

This code complies with what I need, the string variable will get its value of (which is at the beginning of the document before the body)

<%
String permisos= (String)session.getAttribute("permisos");
%>

When it is directly placed in the html it works correctly, when I want to put it in a separate .js file, and then call it with

<script source="ruta.."></script>

It does not work for me, maybe the scriplest <% = permissions > Is it what you can not call in my htlm?

    
asked by angelo1793 07.09.2018 в 18:51
source

1 answer

0

That's right, the js file will include it in the browser (leaving the java server environment) so you could not get java functions out of the jsp context, what I recommend is to save your permissions in a hidden input and read it from javascript, or just make an ajax call to a jsp that returns a json with the permissions you have.

leaving something like that in the body

<input id="permisos" type="hidden" value="<%=permisos%>">

and in Javascript using jquery

var cadena = $('#permisos').val();
    
answered by 08.09.2018 / 00:03
source