4

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 though it explains the idea. Som 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.

6
  • Do you have permission for the other site to allow cross-origin stuff? Commented Jan 4, 2014 at 22:35
  • yes, i do, they're all in my domain Commented Jan 4, 2014 at 22:37
  • If the code is authentic, you have a syntax error (you should quote the URL). Please use the dev console to catch these. Commented Jan 4, 2014 at 22:42
  • Code is not authentic... I tried some other variants and I asked becouse I couldn't find it out, but it was to give the idea, since maybe the text didn't explain enough Commented Jan 4, 2014 at 22:44
  • Look at $.get and its relatives, then. Also, read up about the same-origin policy Commented Jan 4, 2014 at 22:45

1 Answer 1

9

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/

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.