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

473
Vistas
.focus() is not working in Chrome and Edge

For our website, we use Internet Browser before and it was totally fine with no errors but starting from yesterday we migrated to modern browsers and when we click, we get error message Uncaught TypeError: document.hwinv.datatyp.options.focus is not a function in the developer tool. Error is causing in document.hwin.datatyp.options.focus(); line. Before using in Edge, it was totally fine and right now, it is causing that error. May I know how can I fix it? Added JS Fiddle link to check. Also, I saw adding setTimeout but I don't know how that works.

JsFiddle link, U can see the the bottom of the web, there is 2 button and If i click those buttons, causing the error for .focus()

function gonext()
{
 var myIndex = document.hwinv.datatyp.options.selectedIndex;
 var chkvalue=document.hwinv.datatyp.options[myIndex].value;
 if(!(chkvalue))
 {
 document.hwinv.datatyp.options.focus();
 alert("Please select Hardware Type!");
 }
 else
 {
 document.hwinv.step.value='11';
 document.hwinv.submit();
 }
}
<body style="margin=0px;" alink="#0000ff" vlink="#0000ff" bgcolor="#ffffff" window.focus="document.hwinv.datatyp.options.focus();">
<form name="hwinv" method="post" action="inventory.php" target="bottomview">

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Welcome to StackOverflow!

Oh boy. If this is the sort of HTML that your site has, then migrating it to use more "modern" HTML, CSS and JS is going to be quite a long process. But it has to be done, now that IE is officially dead.

Alternative: Use "Internet Explorer Mode" in Edge

You may not be aware that there is a way to switch Edge into running a site almost identically to how IE would have done it. Which might be enough to get your site working again. I found some decent instructions for how to do that.

However, I wouldn't rely on that as a long-term solution. At some point, you are going to need to migrate this site to use HTML5, CSS3, and modern JS. But it might help you out in a pinch.

Fixing your code

To start with, the specific answer to your question about the exception is that the focus() method doesn't exist on the .options property of select inputs. Rather, it exists on the select input object itself.

But we also want to deal with the other antiquated markup in the snippet you posted.

  1. window.focus="..." is not a thing. There are on____ attributes on elements - such as onfocus in your case, but it is considered bad practice to use it, typically. Instead, register an event handler in the JS code like I do below.
  2. style="margin=0px" is wrong. Within inline style attributes, the syntax is property: value, not property=value.
  3. alink, vlink and bgcolor attributes should be replaced with the appropriate CSS, as I have done below. alink corresponds to body a, vlink corresponds to body a:visited and bgcolor corresponds to body { background-color: white; }.

function gonext() {
  var myIndex = document.hwinv.datatyp.options.selectedIndex;
  var chkvalue = document.hwinv.datatyp.options[myIndex].value;
  if (!(chkvalue)) {
    document.hwinv.datatyp.focus();
    alert("Please select Hardware Type!");
  } else {
    document.hwinv.step.value = '11';
    document.hwinv.submit();
  }
}

document.addEventListener('DOMContentLoaded', function () {
  document.hwinv.datatyp.focus();
});
body {
  margin: 0;
  background-color: white;
}

body a {
  color: blue;
}

body a:visited {
  color: blue;
}
<body>
  <form name="hwinv" method="post" action="inventory.php" target="bottomview">
    <select name="datatyp">
      <option>Hammer</option>
      <option>Spanner</option>
      <option>Other</option>
    </select>
  </form>
</body>

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