I have a JS function embedded in my web page, 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 meets what I need, the string variable will get its value from <%=permissions%> (which is found 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, to later call it with
<script source="ruta.."></script>
It doesn't work for me, perhaps the scriptest <%=permissions> is what doesn't allow calling it in my html ?
That's right, the js file will be included in the browser (leaving the java server environment) so you could not take 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 simply make an ajax call to a jsp that returns a json with whatever permissions you have.
leaving something like this in the body
and in Javascript using jquery