Write file in .ASP with JAVASCRIPT conditions

1

How do I write an .asp file if a condition in JAVASCRIPT?

NOTE: I can already write files in a quiet .asp file.

Below the source portion as an example:

<script>

if(1 == 2) { // ISSO É FALSO, PORTANTE ELE DEVE IR PARA O ELSE, POREM ELE FAZ TODO ASP 
    <% 
     oStream.WriteLine "ESCREVE LINHA 2" 

    If (1 = 1) Then 'IF DO ASP, SE 1 = 2, ELE NÃO ESCREVE , ESTÁ OK
        oStream.WriteLine "ESCREVE LINHA 3"

    End If
    %>

} else {
    <% oStream.WriteLine "ESCREVE FIM" %>
}  

So when the javascript condition is false, it does not respect, and does all ASP.

The result of my file is:

...

WRITE LINE 2 // THAT SHOULD NOT HAVE

WRITE LINE 3 // NOR THAT

WRITE AT END ...

I tried to color the entire font for you to test, but I can not get a bug in the editor.

Real Source:     

if (frm['MAPA'].window.ChecarDentroDoPonto('VEICULOS', '{<%=rs("VeiculoID")%>-<%=rsPosicao("VeiculoLogID")%>}', '{<%=strChaveRotaP%>}') == false) {
    document.write('<input type="hidden" name="SQL" value="UPDATE VeiculosLogsAuxiliar SET RotaP=\'A\' WHERE PrefixoView=\'<%=rs("PrefixoView")%>\' AND VeiculoLogID=<%=rsPosicao("VeiculoLogID")%>">');

    <% If IsNull(strEmailNew) = False Then
        If InStr(1, strEmailNew, "@") > 0 Then %>

            document.write('<input type="hidden" name="AlertaEmailVeiculoID" value="<%=rs("VeiculoID")%>">');
            document.write('<input type="hidden" name="AlertaEmailAlerta" value="ROTAP<%=lngRotaPID%>-<%=lngPontoIDAtual%>">');
            document.write('<input type="hidden" name="AlertaEmailEmail" value="<%=strEmailNew%>">');
            document.write('<input type="hidden" name="AlertaEmailTitulo" value="<%=strEmpresa & " - ALERTA - " & rsPosicao("DtLog")%>">');
            document.write('<input type="hidden" name="AlertaEmailMensagem" value="<%="Cliente: " & rs("NomeFantasia") & "<br>" & "Veículo: " & rs("VeiculoID") & " - " & rs("Placa") & " - " & rs("Descricao") & "<br>" & "Atrasado na rota \'" & strRotaPDescricao & "\'" %>">');

        <%
        End If
    End If
    %>

</script>
    
asked by anonymous 12.06.2014 / 16:07

1 answer

2

The Problem:

<script>
if(1==2)
{
alert('entrou na condicao 1');
<%
Response.Write("ASP: condicao01")
%>
}
else
{
alert('entrou na condicao 2');
<%
Response.Write("ASP: condicao02")
%>
}
</script>

In theory, what do you think will appear on the screen? the answer is a javascript error and who knows an alert2

the output is something like this

<script>
if(1==2)
{
alert('entrou na condicao 1');
ASP: condicao01
}
else
{
alert('entrou na condicao 2');
ASP: condicao02
}
</script>

Why?

Because ASP is first rendered on the server, and its html is written. then sent to the browser and then the javascript is executed by the browser ... ie the ASP has already been processed before ...

You should do the same validation in ASP or preferably do the opposite

<%
if 1 = 1 then
   <script>
        //colocar o script javascript aqui
   </script>
else
   <script>
        //colocar o script javascript aqui
   </script>
end if
%>

So the server processes and only sends one or another javascript script ... try to post the entire code so I can give the solution, here I only went through the logic and why.

If you really need to validate first in js, call an external page that writes the log. something like:

 <script>
    if(1==2)
    {
       $("#idElemento").load('gravaLog.asp?formato=1');
    }
    else
    {
       $("#idElemento").load('gravaLog.asp?formato=2');
    }
    </script>

You can also call different pages, etc., etc. This will work.

    
12.06.2014 / 16:53