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

78
Vistas
document.createElement is not working for anchor tags

I was trying to validate some URLs. I have tried using regex and JavaScript URL constructors as well. But I was specifically trying to solve the task with document.createElement('a'). I am following the codes for the task and it works fine in JSbin. But not working in my local editor. Can anyone explain if document.createElement('a') is valid for anchor tags and why there is a result in JSbin and not in my local editor(it shows some error).

P.S Working new with JavaSCript and DOM Manipulation

HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
</head>
<body>
<div id="div"></div>
<script src="test.js"></script
</body>
</html>

JavaScript

function isValidURL(str) {
   var div = document.getElementById('div');
   var a  = document.createElement('a');
   a.href = str;
   a.innerText = "check";
   div.appendChild(a);
   return a.protocol === "http:" || a.protocol === "https:";
}
console.log(isValidURL('file:///Y:/bleh/index.html'));
console.log(isValidURL('https://www.youtube.com/watch?v=iFP-EWO_Q_o'));
console.log(isValidURL('en.wikipedia.org'));

The console results false, true, true accordingly in jsbin. But show error ReferenceError: document is not defined in local editor.

about 4 years ago · Juan Pablo Isaza
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