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

184
Views
How to store content in a div on an external website into a javascript variable?

What I'm looking to do is to get content inside a div on another website (which I do not control) and display the content on my own website. For example, we will use these two sites:

  • mysite.com
  • othersite.com

On othersite.com, there is some text content inside a div that I want to fetch with javascript and update periodically (suppose, check every hour). For example, <div class="some-div">9384</div>.

Now, on mysite.com, I want to output this result into my own div using javascript. I suppose this would effectively be web scraping.

To do this, I would like to save a variable which updates occasionally like every hour.

Using jquery, I could define a variable and pull the content. If it was on the same site, which it is not, I could do this:

/* HTML with content to fetch */
<div class="some-div">9384</div>

/* get the content with jquery */
var getDiv = document.getElementsByClassName('some-div');
var getContent = getDiv.innerHTML;

If it was on the same site, then it would get the content inside the div and store it to the variable getContent. Here, getContent will equal 9384.

Then I could use it anywhere I want, like replacing the content of a blank div on mysite.com with the content fetched from the somediv, like this:

/* HTML where the content will be output */
<div class="output-div"></div>   

/* replace the content with jquery form a stored variable */
var getMyDiv = document.getElementsByClassName('output-div');
getMyDiv.innerHTML = getContent.innerHTML; 

The result would be:

<div class="output-div">9384</div>   

But, the content for the some-div div is not on the same URL (and I don't control the other site).

How do I do the above, but instead of fetching some-div from the current page or site, to instead fetch some-div content from an external site (that I do not control)?

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

0

If you want to use jQuery:

$.get('http://othersite.com', function(data) {
    var $othersite = $(data);

    var getDiv = $othersite.find('.some-div');
    var getContent = getDiv.html();

    var getMyDiv = $('.output-div');
    getMyDiv.html(getContent);
});

If you want to use JavaScript without jQuery:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://othersite.com', true);
xhr.responseType = 'document';
xhr.send();
xhr.onload = function(e) {  
    var othersite = e.target.responseXML;

    var getDiv = othersite.getElementsByClassName('some-div');
    var getContent = getDiv.innerHTML;
 
    var getMyDiv = document.getElementsByClassName('output-div');
    getMyDiv.innerHTML = getContent;
}

about 4 years ago · Juan Pablo Isaza Report

0

In other not to worry about CORS, you can try these steps:

  1. Make an AJAX request to your server via jquery/javascript.
  2. Make a request from your server to othersite.com via your serverside language (eg. PHP).
  3. You can then manipulate the AJAX response any how you like.

Example - Step 1 and Step 3

$.get('mysite.com/some-php-file.php', function(data) {
    let $othersiteHtml = $('<div />').html(data);

    let getSomeText = $othersiteHtml.find('.some-div').text();
    $('.output-div').html(getSomeText);
});

Step 2 - You can put below code in a PHP file on your server.

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "othersite.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

echo $output;

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