Is it possible to make an IF with javascript directly in html?

Asked

Viewed 8,168 times

-5

I have a button that I want to show only if the person logged in is an administrator. But it has to be in JS.

In PHP, I would do so:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>

<body>

    <?php if (loginPermissoes == "admin"){ ?>
        <button id="btnOcorrenciaConcluida">Marcar como concluído</button>
    <?php }else{ ?>

    <?php } ?>


</body>
</html>

How do I do this in Javascript or with jQuery?

Would that be?

<script> if (loginPermissoes == "admin"){ </script>
    <button id="btnOcorrenciaConcluida">Marcar como concluído</button>
<script> }else{ </script>

<script> } </script>
  • <script> your code goes here. </script>

  • @Highlander I know it’s in the inside <script>...rsrsrs, but is done in the same structure as my php example above?

  • And how will you make the JS know if you are admin or not?

  • @bigown when the person logs in gets registered in the variable loginPermissoes.

  • 2

    The JS code does not work, the logic of the two languages is completely different. PHP creates text to send to the browser, JS manipulates the existing page through the GIFT. But how does JS know the value of this variable? needs to know how it intends to solve this. Depending on the case, it makes no sense to solve this in JS. If it makes sense, maybe it’s not just a case of messing with this.

  • I could even answer how to do the display but I would need to know where the information comes from whether it’s admin or not. Without this, it is an abstract problem and not real, then it is difficult to answer.

Show 1 more comment

4 answers

4


Do this only in the front-end, It won’t bring any security. Any malicious person will be able to control things as long as they have the minimum of knowledge, anyway you can use combined with css, thus:

Note: I only used the window.prompt as an example, you can control the variable as you wish.

var btnOcorrenciaConcluida = document.getElementById("btnOcorrenciaConcluida");
var permissoesLogin = window.prompt("Qual permissão?");

if (permissoesLogin == "admin") {
   btnOcorrenciaConcluida.className = "";
} else {
   btnOcorrenciaConcluida.className = "hide";
}
.hide {
    display: none;
}
<button id="btnOcorrenciaConcluida" class="hide">Marcar como concluído</button>

The whole code goes like this:

<html>
<head>
    <style>
    .hide {
         display: none;
    }
    </style>
</head>
<body>
    <button id="btnOcorrenciaConcluida" class="hide">Marcar como concluído</button>

    <script type="text/javascript">
        var btnOcorrenciaConcluida = document.getElementById("btnOcorrenciaConcluida");
        var permissoesLogin = window.prompt("Qual permissão?");

        if (permissoesLogin == "admin") {
           btnOcorrenciaConcluida.className = "";
        } else {
           btnOcorrenciaConcluida.className = "hide";
        }
    </script>
</body>
</html>
  • 3

    This is a solution, I do not know if it solves the problem of AP, but at least it meets the requirements posted.

2

I don’t quite understand what you want. Do you want to use variables made in PHP by JS? Without using PHP in the middle? So I think you want to use Ajax.

Basically I’ve done an old function to make a request Ajax. You can use them:

function Get(t,e,n){var s=new XMLHttpRequest;s.onreadystatechange=function(){4==s.readyState&&200==s.status?(e(s.responseText),s=void 0):s.status>=500&&(s=void 0,n(0))},t+=t.indexOf("?")>-1?"&tsmp="+Date.now():"?tsmp="+Date.now(),s.open("GET",t,!0),s.send()}
function Post(t,e,n,o){var s=new XMLHttpRequest;s.onreadystatechange=function(){4==s.readyState&&200==s.status?(n(s.responseText),s=void 0):s.status>=500&&(s=void 0,o(0))},t+=t.indexOf("?")>-1?"&tsmp="+Date.now():"?tsmp="+Date.now(),s.open("POST",t,!0),s.setRequestHeader("Content-type","application/x-www-form-urlencoded"),s.send(e)}

(b, well, I’ve taken pills before)

And then, well, you need a PHP file that echoes some user value. As an example:

<?php (...) echo Usuario["privilegio"];?>

Then using the function Get above you can take this value that will come out of the PHP file. Are required 3 arguments: [URL do arquivo], [função de sucesso] and [função de erro]

Get("http://localhost:8000/valor.php",function(e){
      alert("Valor: "+e);//alerta "Valor: Usuario[\"privilegio\"]" do arquivo PHP
      if(parseInt(e)==0){//se o privilégio do usuário é igual à 0
         //yay
      }
    },function(){
      alert("Falhou na requisição. Pode ser um delay?")
    }
)

Remember to make the same condition if to the administrator’s action file.

  • 3

    The solution is interesting, but it uses PHP that he says he can’t use. I know it’s not easy to solve this, but the question is clear in this sense.

1

<script type="text/javascript">
    var permissoesLogin = '<?php echo $loginPermissoes; ?>';
    if (permissoesLogin == "admin") {
         //todo
    } else {
         //todo
    }
</script>
  • I can’t use PHP, it has to be all JS.

  • Its validation is not done server-side? You bring the answer from server pro client and does the treatment.

  • This, this solution gives in anyway, if it is to do so, it is better to solve everything in PHP. Although @Tiago may hardly ask you, there is a reason not to use PHP?

  • Amigo @Tiago, it’s impossible - at least I don’t know - you treat values via javascript server. If you want to use javascript, beauty... however the variable permissoesLogin must receive what comes from server (attribute made on server).

  • @bigown, I’m learning to use the intelxdk, only accepts Html5 and js.

  • 2

    So @Marllonnasbeing the problem is that your answer doesn’t solve his problem. It doesn’t really make anything useful even if PHP could be used. Although someone thought so. I avoid negativizing things like that, but this answer does nothing.

  • @bigown, I understood the question as it is being asked: "How to do such validation via javascript/jquery?". I posted one of the N ways to do it via javascript. rs... but apparently he wants to handle server values on the client without putting on the client what comes from the server... this is impossible for me. if he finds out, would love to know rs. Anyway, I think he wants to do the impossible.

  • 2

    But you used PHP. And didn’t display anything.

Show 3 more comments

-1

you will not be able to divert the flow with javascript this way you did.

you can insert the html inside the javascript

<script type="text/javascript">
var btn = '<button id="btnOcorrenciaConcluida">Marcar como concluído</button>';
if (permissoesLogin == "admin") {
     // comando para escrever o btn em alguma tag html
} else {
     // faz outra coisa...
}

  • 2

    And where does this variable come from permissoesLogin? This code also does nothing useful.

  • he informed her in the code he gave of example. The issue apparently talks about the flow deviation with javascript. If the question is about user authentication, I believe there is no way to do it with javascript

  • 1

    I did not deny anything. But your answer does not answer what was asked, and those who did not also think this. Tell us where this variable comes from and do something inside the if and then your answer will be answering the question of fact.

  • @bigown why keep negativizing like this? my question answers exactly what he asked. He didn’t say it was user validation issues. It treats the permissionLogin variable as it wants...

  • this variable comes from the code that he reported. And he treats her as he pleases. If the problem is client-side authentication, you should have specified it in the question. It is impossible, as far as I know, to authenticate client-side users. His problem is flow control, at least that’s what was spelled out in the question.

Browser other questions tagged

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