0

There have been occasions where I have desired to retrieve the HTML of pages other than the one I am typically on. This has happened either when I am using a browser's JavaScript console or when I am writing a bookmarklet. So, given any URL, I wish to retrieve the content of that URL so that I may then use it in, say, a JavaScript variable.

The desired solution cannot use jQuery since that requires loading an external library, which is only optimal in the context of JavaScript that is running in a webpage (as opposed to a console or from a bookmarklet). (Also, adding a script tag to a page just to use in the console is clunky.)

0

2 Answers 2

1

I believe someone has already answered this,

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}

All credits to the author : Link to original answer

Sign up to request clarification or add additional context in comments.

1 Comment

Proper etiquete is flag it as a duplicate instead of adding another answer. It pollutes the site and makes it hard to find good answers to common questions (due to volume)
0

You will undoubtedly run into cross-site scripting limitations on many pages you try. Many servers prevent requests from domains that differ from the domain you're making the request to. So if you go into the console on this site for example, and try to get the google.com homepage, Google well send back an error saying you don't have permission.

Try it yourself by running the following on this page's console:

$.get('https://www.google.com')

Google returns an error.

error from google

You may be able to do this in a browser extension but likely not from the console of any old page.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.