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

166
Vistas
Cannot read property 'split' of undefined jquery

Hello guys I'm trying to create a function to get the full link if it has #mystring is exists for example

www.mydomian.com/#mystring&.........

but when I split it shows me errors

Uncaught TypeError: can't access property "split", data[1] is undefined

   function my_function() {

      var link = window.location +''; // get website link as string www.mydomain.com

      var data = link.split("#");  // start link after # for example www.mydomain.com/#mystring&anotherstring=hello
      
      var widget_type = data[1].split("&")[0]; // to get "mystring" 


         if(widget_type == 'mystring') { // check if "mystring" in the link exists
           var link = window.location.hash.substr(1); // get full link
            console.log(link)
         }
   
      
   }
   my_function();

so how can i check if "#mystring" is exits in my link before do it and fix split error?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If the link doesn't contain #, the data array after splitting will contain only one item which is the link, So you need to check if there is a second item in the data by optional chaining after data[1]

var widget_type = data[1]?.split("&")[0]

Or you can use if condition.

const path = data[1]

var widget_type = '';

if (path) {
   widget_type = path.split("&")[0]; 
}
about 4 years ago · Santiago Trujillo 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