Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

329
Vistas
agregar un fragmento de texto a una variable si no está incluido en la variable

Estoy tratando de poner una condición para verificar si la variable de text comienza con https:// o no. Si no, quiero agregar https:// al comienzo del 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 Respuestas
Responde la pregunta

0

Simplemente verifique si la entrada startsWith lo que está buscando.

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

Como se señaló en los comentarios, el operador ternario condicional se ha utilizado aquí, puede obtener más información al respecto aquí: Operador condicional (ternario)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Un enfoque es:

 // 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}`;

Referencias:

  • Operador condicional .
  • String.prototype.startsWith() .
  • Literales de plantilla .
about 4 years ago · Juan Pablo Isaza Denunciar

0

Intente usar la función include para verificar si la cadena contiene otra cadena.

 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda