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

172
Visualizações
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 Respostas
Responde à pergunta

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 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