Search code examples
javascriptjquerydynamicresponse

javascript request dynamic html without using "window.open()"


I try to get extract some data from a webpage which is dynamic (js changes content at window.onload). A single http get request is thus not enough to get access to the data needed. Is there a way to do this without the need to render the webpage in a window/tab? I have 300 such data requests to make and don/t want 300 tabs to open.

Right now I have this:

var w = window.open(url, '_blank');
//$.get(url)
// cannot be used because the data changes dynamically after beeing loaded (via js)
var data
setTimeout(function () {
  var html = w.document.documentElement 
  data = $("smthg", html)
  w.close()
}, 500)

Note that I need to delay the data extraction until the "dynamic" content is present.

edit: The data is on a 3rd party webpage.


Solution

  • I assume the URLs you are trying to hit are on different domains so you cant use AJAX directly. But what you can do, if you have a server side scripting language available to you, is create your own proxy.

    For example with php:

    <?php
    // proxy.php
    
    $content = 'File not found.';
    $status = 404;
    
    if (isset($_GET['url']) {
    
      // use curl to get the external URL and put it in a var $content
      $ch = curl_init($_GET['url']);
      curl_setopt(CURLOPT_RETURNTRANSFER, true);
      $content = curl_exec($ch);
    
      // you could do some error handling here with the status code/content of the response
      // as needed but we will skip all that for now
    
      if ($content) {
         $status = 200;
      } else {
        $content = 'Error';
        $status = 500;
      } 
    }
    
    HttpResponse::status($status);
    HttpResponse::setContentType('text/html');
    HttpResponse::setData($content);
    HttpResponse::send();
    

    Now you can simply make ajax requests to proxy.php like:

    var data;
    $.get('proxy.php', {url: url}, function (html) {
       data = $("smthg", html);
    });
    

    Of course on the off chance these URLs are not on different domains then you can just use AJAX directly:

    var data;
    $.get(url, function (html) {
       data = $("smthg", html);
    });