Cipher
Cipher

Reputation: 6102

Fetch html page content into a var

Just a small question here, that how do we get fetch the html content via ajax into a variable that I could use later.
Right now, I have a button on the click of which, I fetch another html page simply through load method as follows:
$('#container').load('http://127.0.0.1/someUrl')

I want to get the content into a var instead that I could at a later time use to append to the dom
$('#someContainer').append(someVar)

Upvotes: 1

Views: 250

Answers (2)

Seth
Seth

Reputation: 1373

Using jQuery/JavaScript, you can use AJAX if the file is on the same website:

var somevar;
$.get('myfile.html', null, function(data){
    // data will be the HTML
    somevar = data;
}, 'html');

If the file is from another website, you could try using JSONP, but I would recommend doing a local AJAX request to a PHP script and have PHP make a curl request to get the HTML instead. This will likely handle the request more efficiently and reliably than JSONP.

Upvotes: 1

TGH
TGH

Reputation: 39268

var someVar;

$.get("http://127.0.0.1/someUrl",function(data){
   someVar = data;
});

I would use $.get instead for this

Upvotes: 2

Related Questions