0

I'd like to load a portion of external website content using jQuery and AJAX, as I have read that you can do this. I'd only like to load a portion of the website as explained in this post iframe to Only Show a Certain Part of the Page .

I have full copyright permission to the site I'm trying to load. Thanks, etrey

Community
  • 1
  • 1
etrey
  • 447
  • 3
  • 6
  • 15

2 Answers2

3

For this you can create a div in your main page

<div id='iframediv'>
</div> 

Then in your JS code mention the following line

$('#iframediv').load('https://www.google.co.in #DIVNAME');

Here you have to replace https://www.google.co.in with the target URL. And replace DIVNAME with the target div id in the target page.

UPDATE:

This method will not work if you are accessing a page in different domain because of security issues. In that case you can do the following

  1. create a New Server-side Page (say GetContent.aspx) in your web application (so that the domain will be same).
  2. Then on server-side you can load the required page (from a different domain) and extract the required section. If you are not able to extract the section, load the complete page.
  3. Then load this content to the New Server-side Page (say GetContent.aspx)
  4. Now you can show this GetContent.aspx using .load function. If you are using the complete page (without extracting) then use the following code for loading the required content div.

    $('#iframediv').load('https://www.google.co.in #DIVNAME');

Wolf
  • 2,150
  • 1
  • 15
  • 11
  • Can it be an external domain? – etrey Jan 02 '13 at 02:23
  • In case of external domains, you can do something as mentioned in the answer update – Wolf Jan 02 '13 at 10:20
  • On the `GetContent` page would you reccomend an html iframe or the jquery `.load` method? @Wolf – etrey Jan 02 '13 at 21:24
  • Use `.load` method if you want to load the content at run time. If you want to load the content along with the page load, then `iframe` will be a simple option – Wolf Jan 02 '13 at 21:47
  • Go figure `.load` on a `GetContent` page presents the same non-loading error because it is a different domain still. However, if I do a `.load` of a `
    ` I am not sure how that will work because the `iframe` code does not appear to pull in the `div`'s it just presents the page in a frame. This answer would work http://stackoverflow.com/questions/2176342/embedding-part-of-a-web-site#answer-2225401, however I had a problem scraping with styles. @Wolf
    – etrey Jan 02 '13 at 21:55
  • On `GetContent.aspx` page, you need to load the content using server side code. `C#/vb` if you are using ASP.net – Wolf Jan 02 '13 at 22:05
  • I'm using PHP, do you know what that would look like? @Wolf – etrey Jan 02 '13 at 22:07
  • 1
    [embedding-part-of-a-web-site#answer-2225401](http://stackoverflow.com/questions/2176342/embedding-part-of-a-web-site#answer-2225401) In this answer check the section "Employing simplexml and xpath" Just copy paste the code. that should work – Wolf Jan 02 '13 at 22:21
  • [link](http://goo.gl/alCFk) Getting a weird error with that code. Thank you for all of your help! @Wolf – etrey Jan 02 '13 at 22:44
  • Seems like the page you are using (http://google.com/news) is not having a valid xml structure. It is possible because of the unstructured tags like
    etc. Check my updated answer.
    – Wolf Jan 03 '13 at 06:57
1

You state that this is an external website. If the website was within the same domain, you could use jquery's load method to do this

$('#iframediv').load('https://www.google.co.in #DIVNAME');

If the page is external, you won't be able to do that. You could look at the external page and see if there is a named anchor. If there is, you can use that anchor in the src of the iframe to cause the iframe to start out scrolled to that position. I don't think it will get you exactly what you want but it's the closest you will be able to get with the page being on an external domain.

<iframe src="http://externalsite.com/page.html#nameofanchor"/>
Joseph Yancey
  • 1,498
  • 1
  • 9
  • 7
  • 1
    Is there anyway to use `.load` or a similar method to load a portion of a website in an iframe that is a different domain? @Wolf – etrey Jan 02 '13 at 02:16