How to link to HTML from an external web page

I apologize in advance for the rudimentary question.

I have webpage A that has a link to webpage B. I need to find the link to webpage B (simple enough) and then save the HTML from webpage B in a variable in my javascript script.

To save the HTML from webpage A, I know this is simple:

html_A = document.body.innerHTML;

How to save HTML from webpage B? I believe that I need to use AJAX correctly? Or can I do this using javascript? And if this is the first, let me just assume that the server for webpage B allows this.

Thank you in advance!

+4
source share
2 answers

HTML , , Cross-Origin Request Blocked. YQL. :

//This code is located on Website A
$(document).ready(function() {
    var websiteB_url = 'http://www.somewebsite.com/page.html';
    var yql = '//query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + websiteB_url + '"') + '&format=xml&callback=?';
    $.getJSON(yql, function(data) {
        function filterDataCUSTOM(data) {
            data = data.replace(/<?\/body[^>]*>/g, '');// no body tags
            data = data.replace(/[\r|\n]+/g, ''); // no linebreaks
            return data;
        }
        if (data.results[0]) {
            var res = filterDataCUSTOM(data.results[0]);
            $("div#results").html(res);
        } else {
            console.log("Error: Could not load the page.");
        }
    });
});
+3

, - B - .

,

$.get("/uri/to/webpage/b").then(function(html) {
     //do something with the html;
});

, html ajax .then(...). .

, , . $.fn.load() ( SOP) iframes ( SOP), .

, , html , - -, . , , -.

+2

All Articles