Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get another page's content in a variable with ajax

Is there a way to set a javascript variable as the content of another HTML page?

I tried:

var X = $(http://www.website.com/home).html()

but it didn't return anything.... Even tho it explains the idea... so... can anyone tell me how to do so please? Also the content of a certain id or class in that website, something like:

var X=$(http://www.website.com/home "#id").html()

It would really help me, thanks in advance!

like image 899
towc Avatar asked Feb 15 '23 05:02

towc


1 Answers

It sounds like you're looking for this:

$.get('otherPage.html').then(function(responseData) {
  //responseData is the contents of the other page. Do whatever you want with it.
  $('#someElem').append(responseData);
});

Live demo (click).

$.get() is a shorthand method for jQuery's $.ajax(). http://api.jquery.com/jquery.ajax/

like image 155
m59 Avatar answered Feb 17 '23 03:02

m59