简体   繁体   中英

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)

var someVar;

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

I would use $.get instead for this

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM