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

335
Visualizações
add a piece of text to a variable if not included in the variable

I am trying to put a condition to check if the text variable starts with https:// or not. If not, I want to add https:// to the start of text.

 document.addEventListener("keydown", function (event) {
if (event.keyCode === 13) {
    var text = "https://" + document.getElementById("input").value;
    document.getElementById("iframe").src = text;
    document.getElementById("iframe").height = "100%";
    document.getElementById("iframe").width = "100%";
    document.getElementById("content").innerHTML = '';
}

});

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

0

Simply check if the input startsWith what you're looking for..

let input = document.getElementById("input").value;
let text = input.startsWith("https://") ? input : "https://" + input;

As noted in the comments, the conditional ternary operator has been used here, you can find out more about it here: Conditional (ternary) operator

about 4 years ago · Juan Pablo Isaza Relatório

0

One approach is:

// here we cache the value of the <input> element:
let value = document.getElementById("input").value,
    // we define the protocol we're looking for:
    protocol = 'https://',
    // we then assign the text, using a template-literal string,
    // first we use a conditional operator:
    // which returns a Boolean (true/false), if the
    // String we test starts with the supplied String
    // ('protocol'); if it does we return an empty string
    // and if not we return the protocol variable; and then
    // that's concatenated with the existing 'value'
    // variable:
    text = `${value.startsWith(protocol) ? '' : protocol}${value}`;

References:

  • Conditional operator.
  • String.prototype.startsWith().
  • Template literals.
about 4 years ago · Juan Pablo Isaza Relatório

0

Try to use includes function to check if the string contains another string.

document.addEventListener("keydown", function (event) {
  if (event.keyCode === 13) {
    let text = document.getElementById("input").value;
    let src = text.includes("https://") ? text : "https://"+text;
    document.getElementById("iframe").src = src;
    document.getElementById("iframe").height = "100%";
    document.getElementById("iframe").width = "100%";
    document.getElementById("content").innerHTML = '';
  }
});
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