What alternative to replace ASP codes with Javascript?

Asked

Viewed 238 times

2

I have this code in asp and javascript.

<%if trim(ind_tipo_proposta_pj) = "S" then%>
                var onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);
            <%else%>
                var onum_seq_proposta_ts    = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);    
            <%end if%>

I need to make it cleaner without the tags asp. The thing is, I have a lot of doubt when it comes to asp. Well, a few days ago, my colleague Tobymosque helped me a lot in doing something similar. I am not here asking you to do it for me, otherwise I will always be asking, but a hint or a line to be followed. My attempt was in this:

if()
var onum_seq_proposta_pj_ts = "<%= trim(nome_campo_cod_ts) %>" || "";
var onum_seq_proposta_ts    = "<%= trim(nome_campo_cod_ts) %>"

I couldn’t finish the if and I also don’t know if the ou is correct. See that there is a if in the asp, if legal, loads a variable or loads another variable. I wonder if I kill the if of asp and put a if of js, if it will give tilt. I could also make a ternary, but how to do it in a statement of a variable. I would if, but in the statement I don’t know if it’s possible(I don’t think so). As I do, or rather, which way to go?

Making my attempts here I arrived at this code, but I do not know if it is right, because to test I will have to change much more things, but just tell me. Is this the way? I didn’t kill 100% the asp, but gave an optimized in relation to the asp.

var onum_seq_proposta_pj_ts = null;
var onum_seq_proposta_ts    = null;
<%if trim(ind_tipo_proposta_pj) = "S" then%>
   onum_seq_proposta_pj_ts = "<%= trim(nome_campo_cod_ts) %>" || onum_seq_proposta_ts = "<%= trim(nome_campo_cod_ts) %>";
<%end if%>

Okay, actually the variables txt_prefixo and txt_sufixo are variables. They already know how to treat. See how they are declared:

    var txt_prefixo = '';
    var txt_sufixo  = '';

  try{
       txt_prefixo = '.document.form01.';
       txt_sufixo  = '';
       ......

Tobymosque, this is how I should use Object?

var txt_prefixo = '';
var txt_sufixo  = '';
window["objeto"] = null;


        var nome_campo_cod_ts_id = "<%= trim(nome_campo_cod_ts) %>";

        var onum_seq_proposta_pj_ts = null;
        var onum_seq_proposta_ts = null;
        var txt_chamada = "";

        var onum_seq_proposta_pj_ts_id = "<%= trim(ind_tipo_proposta_pj)%>" == "S";

        if(onum_seq_proposta_pj_ts_id)
            window[txt_chamada][txt_prefixo][onum_seq_proposta_pj_ts][txt_sufixo];
        else
            window[txt_chamada][txt_prefixo][onum_seq_proposta_ts][txt_sufixo];

In fact I’m having great difficulty eliminating this try..catch, i.e., replace with cleaner javascript code:

try {
            txt_prefixo = '.document.form01.';
            txt_sufixo  = '';
            <%if trim(ind_tipo_proposta_pj) = "S" then%>
                var onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);
            <%else%>
                var onum_seq_proposta_ts    = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);    
            <%end if%>
        } catch (e) {
            txt_prefixo = '.document.all[\'';
            txt_sufixo  = '\']';
            <%if ind_tipo_proposta_pj = "S" then%>
                var onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);
            <%else%>
                var onum_seq_proposta_ts    = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);
            <%end if%>
        }

That one try..catch is inside that if in ASP:

<%if trim(nome_campo_cod_ts)<> "" then%>
        try {
            txt_prefixo = '.document.form01.';
            txt_sufixo  = '';
            <%if trim(ind_tipo_proposta_pj) = "S" then%>
                var onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);
            <%else%>
                var onum_seq_proposta_ts    = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);    
            <%end if%>
        } catch (e) {
            txt_prefixo = '.document.all[\'';
            txt_sufixo  = '\']';
            <%if ind_tipo_proposta_pj = "S" then%>
                var onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);
            <%else%>
                var onum_seq_proposta_ts    = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>' + txt_sufixo);
            <%end if%>
        }
    <% else %>
        try {
            txt_prefixo = '.document.form01.';
            txt_sufixo  = '';
            <%if ind_tipo_proposta_pj = "S" then%>
                var onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%>num_seq_proposta_pj_ts' + txt_sufixo);
            <%else%>
                var onum_seq_proposta_ts    = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%>num_seq_proposta_ts' + txt_sufixo);
            <%end if%>
        } catch (e) {
            txt_prefixo = '.document.all[\'';
            txt_sufixo  = '\']';
            <%if ind_tipo_proposta_pj = "S" then%>
                var onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%>num_seq_proposta_pj_ts' + txt_sufixo);
            <%else%>
                var onum_seq_proposta_ts    = eval(txt_chamada + txt_prefixo + '<%Response.Write prefixo%>num_seq_proposta_ts' + txt_sufixo);
            <%end if%>
        }
    <%end if%>

2 answers

2


Why don’t you write the values of the variables ind_tipo_proposta_pj nome_campo_cod_ts and prefixo in fields HTML of the kind hidden, as soon as the ASP is executed, then you can manipulate with javascript. Remember to put the fields before the code JavaScript so the server has already "written" the fields with the values and you will be able to manipulate them then with JavaScript:

<input type="hidden" name="ind_tipo_proposta_pj" id="ind_tipo_proposta_pj" value="<%= trim(VARIAVEL_DO_ASP_TIPO_PROPOSTA) %>"/>
<input type="hidden" name="nome_campo_cod_ts" id="nome_campo_cod_ts" value="<%= trim(VARIAVEL_DO_ASP_NOME_CAMPO_COD_TS) %>"/>
<input type="hidden" name="prefixo" id="prefixo" value="<%= trim(VARIAVEL_DO_ASP_PREFIXO) %>"/>

Then you use everything in Javascript:

var onum_seq_proposta_pj_ts = null;
var onum_seq_proposta_ts = null;
var txt_chamada = "";
var txt_prefixo = "";
var txt_sufixo = "";

var campo_ind_tipo_proposta_pj_que_veio_do_asp = document.getElementById('ind_tipo_proposta_pj').value;
var campo_nome_campo_cod_ts_que_veio_do_asp = document.getElementById('nome_campo_cod_ts').value;
var campo_prefixo_que_veio_do_asp = document.getElementById('prefixo').value;
var prefixo_do_banco_de_dados = (campo_prefixo_que_veio_do_asp + "" + campo_nome_campo_cod_ts_que_veio_do_asp)

if (campo_ind_tipo_proposta_pj_que_veio_do_asp == "S") {
  eval(txt_chamada + txt_prefixo + prefixo_do_banco_de_dados + txt_sufixo);

} else {
  eval(txt_chamada + txt_prefixo + prefixo_do_banco_de_dados + txt_sufixo);
}
<input type="hidden" name="ind_tipo_proposta_pj" id="ind_tipo_proposta_pj" value="S" />
<input type="hidden" name="nome_campo_cod_ts" id="nome_campo_cod_ts" value="" />
<input type="hidden" name="prefixo" id="prefixo" value="document.write('ok');" />

The problem there, my friend, is that both for the individual and for the legal person the variable onum_seq_proposta_pj_ts and onum_seq_proposta_ts will be equal. Because if you look at the corresponding code in the script ASP original, the variables are the same <%Response.Write prefixo%><%Response.Write nome_campo_cod_ts%>

  • I didn’t think about it. It can be a good exit yes, it is much more readable. It can be a path or a line to be followed. If it is a PF proposal, this field should come with "N". If I put value on the page as "S", as it would be if it is a PF proposal?

  • In fact if you look further up the right is to put the value with the printing of the contents of the variable that is in the ASP. This proposal type variable is in the ASP (you read from the database or a text file, for example, using ASP), or is in a field HTML and this page receives this variable ?

  • It comes from the bank. The problem is that the site is very large, this is just a module for which we were hired to provide maintenance. The site is very old. That question I have. If you set value to "S" and enter "N", it changes value, right?

  • I think you’re missing @pnet. I just fixed the field in the example code, so you can test it. But in your there, on your page, you will print the value of the result of the variable that came from the database, I will call it TIPO_DE_PROPOSTA and do the following: <input type="hidden" name="ind_tipo_proposta_pj" id="ind_tipo_proposta_pj" value="<%= trim(**TIPO_DE_PROPOSTA**) %>"/>

  • Okay, I get it now. In this case he himself comes from the BD(ind_tipo_proposta_pj)

  • OK I edited the code because I understood what its original code proposed. It besides checking the value of the variable ind_tipo_proposta_pj is legal entity "S", the code JavaScript of the conditions if and else executes the cluster of other variables, some of the bank (understanding that what comes from your ASP came from the bank) and are they nome_campo_cod_ts and prefixo, and others of the JavaScript and it’s them txt_chamada, txt_prefixo and txt_sufixo.

Show 2 more comments

2

pnet, taking advantage of the idea of Maicom, you can put the variables inside a tag script in your ASP page:

<script type="text/javascript">
    var asp = {};
    asp.ind_tipo_proposta_pj = "<%= ind_tipo_proposta_pj %>";
    asp.prefixo = "<%= prefixo %>";
    asp.nome_campo_cod_ts = "<%= nome_campo_cod_ts %>";
</script>

But by doing so, you will face the same problem that by putting input:Hidden, the user can change the value from ASP.

Then you can do the following to lock these values:

<script type="text/javascript">
    var asp = {};
    Object.defineProperty(asp, "ind_tipo_proposta_pj", { value: "<%= ind_tipo_proposta_pj %>", writable: false, enumerable: true, configurable: true });
    Object.defineProperty(asp, "prefixo", { value: "<%= prefixo %>", writable: false, enumerable: true, configurable: true });
    Object.defineProperty(asp, "nome_campo_cod_ts", { value: "<%= nome_campo_cod_ts %>", writable: false, enumerable: true, configurable: true });
</script> 

Now that you have all these variables in memory, you can work on a separate *.js.

var onum_seq_proposta_pj_ts = null;
var onum_seq_proposta_ts = null;
if (asp.ind_tipo_proposta_pj.trim() === "S")
    onum_seq_proposta_pj_ts = eval(txt_chamada + txt_prefixo + asp.prefixo + asp.nome_campo_cod_ts + txt_sufixo);
else
    onum_seq_proposta_ts = eval(txt_chamada + txt_prefixo + asp.prefixo + asp.nome_campo_cod_ts + txt_sufixo);

The problem of the above code is the use of Eval, I particularly don’t like to dynamically mount Avascript this way, so let’s address an alternative.

When we talk about Java in the browser, all global variables/functions are actually properties of the window object.

Then all the statements below are equivalent:

var objeto = null;
window.objeto = null;
window["objeto"] = null;

in the case of a dynamic call, the first two forms do not interest us, but note the third, I can pass the variable name as an object.

then instead of doing something like this:

var prefixo1 = "teste1.";
var prefixo2 = "teste2.";
var propriedade = "teste3";
var valor = eval(prefixo1 + prefixo2 + propriedade);

we might have something like this:

var prefixo1 = "teste1";
var prefixo2 = "teste2";
var propriedade = "teste3";
var valor = window[prefixo][prefixo2][propriedade];

this way you can evaluate the value of this property through the browser watch (in the case of Chrome/operates the "Chrome" Devtools [F12]).

  • Okay Toby, I’m really doing evaluations and tests. I’ve been following the previous model that you helped me(mounted). This approach, my leader liked it very much and said to follow this pattern. Maicon is not out of this approach. This function is very big, I posted a small part only, because I have to understand, learn and do, so I didn’t post everything. I will follow the approach of both, make my due changes and test.

  • Just correcting an earlier piece of information. I answered that the Asp calls come from the bank, but no, they are coming from the calling form, the fields, like a Checkbox and so on... As an allowance only.

Browser other questions tagged

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