Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
¿Por qué DOMParser deja de existir fuera de esta declaración if?

Estoy usando DOMParser() para crear un nuevo modelo de objeto de documento desde una página web remota para poder crear un raspador web en el navegador. Este es el código que tengo para recuperar la página web remota.

 function getDOM(url){ // Variables let dom = new Document; let xhr = new XMLHttpRequest(); let cors = "https://cors-anywhere.herokuapp.com/" // Create and send the XMLHttpRequest xhr.open('GET', cors + url, true); // xhr.responseType = 'document'; // This was necessary but then i changed // the way i was accomplishing things and it isnt necessary anymore, I do not // know why. // This loads the xhr request into a new DOMParser xhr.onreadystatechange = function () { if (xhr.readyState === 4) { dom = new DOMParser().parseFromString(xhr.responseText, 'text/html') // This line of code is capable of printing the value of the "productTitle" // element to the console, but if i try this same line of code outside of // this if statement it returns an error saying "cannot read // properties of null" console.log(dom.getElementById('productTitle').innerText); } } // This sends the request to the specified url xhr.send(null) //Returns the new DOMParser to the product class return dom; }

El problema es que no puedo manipular ni acceder a ninguno de los contenidos del objeto "dom" fuera de la declaración if en la que existe "dom = new DOMParser().parseFromString(xhr.responseText, 'text/html')". ¿No debería asignarse el documento analizado al elemento "dom" y ser accesible en el resto de la función? Estoy accediendo a los datos con éxito en la instrucción if a través del objeto "dom" al que he asignado los resultados de DOMParser().

Editar: Aunque esta pregunta Enlace a otra pregunta que es similar está tratando de resolver el mismo problema técnico que tengo, las soluciones dadas usan AJAX, jQuery y otras tecnologías que no estoy usando. Solo estoy usando javaScript simple y, por lo tanto, necesito una solución que no se proporciona en esa pregunta.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!