TemporaryFix
TemporaryFix

Reputation: 2186

Get html from a page instead of loading the page

I have a button that loads a page with window.open();

Instead of loading the page, is it possible to get the html from the page without opening it?

Upvotes: 1

Views: 84

Answers (4)

K3V
K3V

Reputation: 282

Please use the jquery load method.

According to the docs,

This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data. This means that most uses of the method can be quite simple.

.load( url [, data ] [, complete ] )

url Type: String; A string containing the URL to which the request is sent.

data Type: PlainObject or String; A plain object or string that is sent to the server with the request.

complete Type: Function( String responseText, String textStatus, jqXHR jqXHR ); A callback function that is executed when the request completes.

Ref: Click Here for jquery documentation : Another Stack overflow question on the same lines

Upvotes: 0

jeremywoertink
jeremywoertink

Reputation: 2341

As the others have said, AJAX (Asynchronous JavaScript And XML) is the way to go. Libraries like jQuery provide an easy to use way of doing this. Outside of the libraries, JavaScript has a XMLHttpRequest object that you can use to do this same thing.

The idea is make a request to a page, and return the markup on that page in some callback then do what you wish with that markup.

Here is an example:

var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
  if(xmlhttp.readyState==4 && xmlhttp.status==200) {
    // the ajax request is done, and the server responded with the html page
    // log the result from the ajax request
    console.log(xmlhttp.responseText);
  }
}
xmlhttp.open("GET","myPage.html",true);
xmlhttp.send(); 

EDIT

If you own both domains, the one making the request, and the one being requested, then you can use CORS. This will tell the other server to allow requests from the first domain.

If you don't own both domains, or have access to both sides, then this becomes a lot more difficult. You can either make an HTTP request from your server side, or check out some other answers on here

Upvotes: 1

Oliboy50
Oliboy50

Reputation: 2721

Using ajax and jQuery:

$.get( "myNewPage.html", function( data ) {
  $( "#myNewPage" ).html( data );
});

Upvotes: 1

Johannes Reuter
Johannes Reuter

Reputation: 3547

You may want to look into AJAX-Requests: https://developer.mozilla.org/de/docs/AJAX

Upvotes: 0

Related Questions