20

Say I have a page a.html and want to retrieve a text element's text using it's id <p id="name">NAME</p> element from another page b.html.

4
  • Request the page from an iframe, then get the element. But wouldn't you rather just have the server return the data you want? Commented Oct 15, 2012 at 15:38
  • possible duplicate of stackoverflow.com/questions/3396542/… Commented Oct 15, 2012 at 15:38
  • @kmb385: As long as the second page is on the same domain, it's totally doable. Commented Oct 15, 2012 at 15:40
  • 6
    Since you didn't ask for jQuery, it should be noted that you do not need to require the user to download and evaluate the entire jQuery library for simple XHR support. Commented Oct 15, 2012 at 15:44

3 Answers 3

24

Using jQuery's get method, it's pretty simple:

$.get('a.html', null, function(text){
    alert($(text).find('#name'));
});

Raw XHR Request (by popular demand):

var request = new XMLHttpRequest();

request.addEventListener("load", function(evt){
    console.log(evt);
}, false);

request.open('GET', 'a.html', true),
request.send();
Sign up to request clarification or add additional context in comments.

9 Comments

jQuery has no $.load method, and it's overkill to load a large library just to do a simple XHR request.
@user1689607 really?! is it overkill if the client's browser has jQuery already in its cache?
@Xander: How do you know if it's in the cache?
@user1689607 — Wrong, it does. While it is probably overkill just to make an XHR request, it isn't to make an XHR request, extract data from the resulting HTML document, and add that data to the DOM of the current page.
Absolutely, if OP has other uses, then it may be worth the download, but I wouldn't be too quick to assume there's such a high probability of being cached. Again, each CDN hosts dozens of versions. There are plenty of sites that use one of the old (and maybe even one of the unminified) versions. So it's really hit or miss.
|
2

You need to:

  1. Fetch the content of the page (you imply that Same Origin Policy won't be a problem, so I won't discuss working around that).
  2. Parse that into an HTML DOM
  3. Extract the part of that DOM you care about
  4. Do something with that DOM

This is quite a lot of work. jQuery will do most of the work for you:

jQuery('#element_to_load_content_into').load('a.html#name'); // Note use of fragment identifier

3 Comments

@PrathameshDoke — Nothing has fundamentally changed since I wrote this answer.
This "loads" a webpage partially, I see that a lot of body elements do not exist in the element I loaded the content into. Any idea how to make it load everything?
@Shayan — I've no idea what you are doing or to what data. Try asking a new question, with an minimal reproducible example and reference this for context.
-1

If the page is on same domain, you can use jQuery.ajax() or jQuery.load()

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

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.