datatest
datatest

Reputation: 473

Scraping html and displaying it on another site

I have a header that I want to share between two sites. They are two separate sites, so I was wondering if its possible to create a javascript, that will pull from site A and display it exactly on site B

basically something like this, but having it display instead of just parsing

function fetchPage(url) {
    $.ajax({
        type: "GET",
        url: url,
        error: function(request, status) {
            alert('Error fetching ' + url);
        },
        success: function(data) {
            parse(data.responseText);
        }
    });

Upvotes: 0

Views: 51

Answers (1)

THEtheChad
THEtheChad

Reputation: 2432

You have all the code you need right there. Just replace your "parse" function with a jQuery wrapper and select the section of the page you want. Keep in mind, this will only work if you're using the same stylesheet on both pages. If not, you'll have to pull in a copy of the styles as well.

function fetchPage(url) {
    $.ajax({
        type: "GET",
        url: url,
        error: function(request, status) {
            alert('Error fetching ' + url);
        },
        success: function(data) {
            $(data.responseText).find('#yourHeader').prependTo(document.body);
        }
    });
}

Upvotes: 1

Related Questions