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

225
Views
Get text from a specific class from a link

Using javascript, I'm trying to open a specific page, so I'm using window.open(). I want to search values and store them, values that are associated with a specific class. I have been searching a lot but I can't find out how to do that, I tried window.open(#link).getElementsByClassName but that doesn't work, nor link.querySelectorAll. Has anyone got any tips?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

/* JavaScript */

class webpage {
  constructor(url) {
    this.url = url;
    this.frame = document.createElement("iframe");
    this.frame.setAttribute("style", "display: none; position: absolute; left: -99999px; top: -99999px;");
    document.body.appendChild(this.frame);
  }
  load() {
    return new Promise(function(resolve, reject) {
      var http = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
      http.open("GET", this.url);
      http.addEventListener("load", function() {
        if(http.status == 200 && http.readyState == 4) {
          this.frame.contentWindow.document.write(http.responseText);
          resolve();
        } else if(http.readyState == 4) {
          reject(Error("Something went wrong"));
        }
      }.bind(this));
      http.addEventListener("error", function() {
        reject(Error("Something went wrong"));
      });
      http.send();
    }.bind(this));
  }
  get(query) {
    return (this.frame.contentWindow.document || this.frame.contentDocument).querySelectorAll(query);
  }
}

I have created a class called 'webpage'. For example, you can build your code like this:

/* JavaScript */

var page = new webpage("somepage.net/");// Only works for other domains if the CORS header 'Access-Control-Allow-Origin' is set
page.load().then(function(){
  var list = page.get("div.className"),
      element = list[0];
  alert(element.innerHTML);
});

however, it only works within your own domain or one for which 'Access-Control-Allow-Origin' is allowed. More about 'Access-Control-Allow-Origin' here.

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