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

188
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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