Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
Changing javascript eval method to another solution

I try to get rid of an ugly javscript eval method (Cause we all know it is unsecure). I have the following problem. I build a dynamic searchstring. Depends on the TLD a user decided to search for.

Here is my code:

 if (tld == 0) {
            var searchString = 'value.tld != ""';
        }
        if (tld == 1) {
            var searchString = 'value.tld == "de"';
        }
        if (tld == 2) {
            var searchString = 'value.tld == "com" || value.tld == "net" || value.tld == "org" || value.tld == "info" || value.tld == "biz"';
        }
        if (tld == 3) {
            var searchString = 'value.tld == "io"';
        }

Depending on the search parameter 'searchstring', I build this routine with eval:

if (eval(searchString)) {
    // Do something special, depends on the tld variable
}

How can i rebuild this without using 'eval'. The premission is, that the first part of the code is beeing untouched.

Thanks in advance

Nick

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

How about:

let choices = {
  1: ['de'],
  2: ['com', 'net', 'org', 'info', 'biz'],
  3: ['io']
};

function check(tldparam) {
  if (tld === 0) {
     return value.tld !== "";
  } else {
    return tld === tldparam && choices[tldparam].includes(value.tld);
  }
}

And we test it like:

// Got this value from somewhere
let tld = 2;
let value = {tld: 'net'};

// This is my checking criterion
let tldparam = 2;

if (check(tldparam)) {
    // Do something special, depends on the tld variable
}

Does it serve your purpose?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda