Validation of HTML variable

Asked

Viewed 72 times

0

Good afternoon guys, all right?

I received an internal project and I’m not getting to finish.

I have this excerpt from an HTML page:

    <span style='font-size:9.0pt;
    font-family:"Calibri",sans-serif;
    color:black>'>
    Apresente este QRCode no terminal de autoatendumento. Ele também está anexo. <BR>
    Identifique-se na <b><span style=font-size:11.0pt;>%%nomePortaria%%</b>, conforme ilustração abaixo.
    </span></span></p>

Just below this excerpt, is displayed a gif of the ordinance selected through the code below:

    <p align=center>
    <img name="imagem1" width=360 height=208>
    </p>

The question is: I need the gif to be changed according to the parameter passed in the %%namePortaria%% variable. I created this very simple code to see if it would work, but it didn’t roll...

<script>
function srcImage(path) {
    document.imagem1.src = path;
}

window.onload = function() {

    if(%%nomePortaria%%  == 1){
        srcImage('85.gif');
    } 
    else if(%%nomePortaria%% == 2){
        srcImage('86.gif');
    }
    else{
        srcImage('84.gif);
    }
}   
</script>

The images are in the same file folder, just for testing... in the future, I will change the paths to a correct image url.

Thank you guys!

  • Note that this variable is coming from a backend, correct?

  • This... is that actually I do not have access to the backend... just passed me this code and asked to do this... :/

2 answers

0

Hey, there, Arildo. I don’t really understand the scope of what you want, but only with HTML you don’t solve. The script you declared is Javascript and it is in it that you have to operate. You var need to have a variable to handle in IF. You’ll also need some way to dynamically change the variable. What did you think?

<script type="javascript">
var nomePortaria = "1";
function srcImage(path) {
    document.imagem1.src = path;
}

window.onload = function() {

    if(nomePortaria  == 1){
        srcImage('85.gif');
    } 
    else if(nomePortaria == 2){
        srcImage('86.gif');
    }
    else{
        srcImage('84.gif);
    }
}   
</script>

0

What happens is you’re mixing code backend with javascript.

A solution would be forçar this variable that comes from the backend be transformed into a variable javascript.

Put that line on script above the import of the code you are creating.

<script> var nomePortaria = %%nomePortaria%% </script>

And your validation script:

<script>
function srcImage(path) {
    document.imagem1.src = path;
}

window.onload = function() {

    if(nomePortaria  == 1){
        srcImage('85.gif');
    } 
    else if(nomePortaria == 2){
        srcImage('86.gif');
    }
    else{
        srcImage('84.gif);
    }
}   
</script>

Remember to put the script validation below the script in which we created the variable.

Browser other questions tagged

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