How do you put only a certain section of a website

2019-03-10 05:30发布

问题:

I'm looking to put only a small part of a website into an iframe. How would I do this? Usually when I set up a iframe for a website (lets say yahoo) it gets the whole website... Lets say I only wanted a small portion of the website, how would I do this?

Is it possible to put margins on an iframe of a website?

I'm looking to put an iframe of a website on my website. (if that makes sense)

Thanks in advance :D

回答1:

In most of the cases the reference is external and you don’t have control over the external page. Thus you’ve to scroll the IFRAME content to the desired position. This of course is impossible. Yeah, there are some JavaScript hacks, but they’re all a bad solution, because the scrolling occurs only after the page is loaded. The Solution

You can wrap the IFRAME into a div and scroll the DIV content using absolute TOP and LEFT CSS properties.

Here’s an example:

#my-div
{
    width    : 400px;
    height   : 200px;
    overflow : hidden;
    position : relative;
}

#my-iframe
{
    position : absolute;
    top      : -100px;
    left     : -100px;
    width    : 1280px;
    height   : 1200px;
}

Here you have one DIV with dimensions 400x200px. Now by moving the IFRAME within it you can position it on the right place.

<div id="my-div">
<iframe src="http://www.example.com" id="my-iframe" scrolling="no"></iframe>
</div>


回答2:

This can't be reliably done due to same origin policy and related iframe restrictions.

.. the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy .. prevents access to most methods and properties across pages on different sites.

If it was your website in your website [or a proxy to the target site] then JavaScript in the parent could modify the DOM or CSS of the embedded iframe as desired ..

If the target website is willing to communicate data through another means (including XDR/XHR+CORS), then those could potentially be used as hack-a-bouts as well. However, there is no general solution for this task.

Even jQuery.load (which uses XHR) is limited by the same origin policy:

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.



回答3:

An <iframe> gives you a complete window to work with. The most direct way to do what you want is to have your server give you a complete page that only contains the fragment you want to show.

As an alternative, you could just use a simple <div> and use the jQuery load function to load the whole page and pluck out just the section you want:

$('#target-div').load('http://www.yahoo.com');

There may be other things you need to do, and a significant difference is that the content will become part of the main page instead of being segregated into a separate window.


You won't be able to manipulate the URL to get only a portion of the page. So what you'll want to do is grab the page contents via the server-side language of your choice and then parse the HTML. From there you can grab the specific DIV you are looking for and then print that out to your screen. You could also use to remove unwanted content.

With PHP you could use file_get_contents() to read the file you want to parse and then use DOMDocument to parse it and grab the DIV you want.

Here's the basic idea. This is untested but should point you in the right direction:

$page = file_get_contents('http://touch.facebook.com');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
    // Loop through the DIVs looking for one withan id of "content"
    // Then echo out its contents (pardon the pun)
    if ($div->getAttribute('id') === 'content') {
         echo $div->nodeValue;
    }
}


回答4:

<iframe> loads the full website another way to do is a simple way

<iframe src="http://site.com/#content"></iframe> 

where #content is the content divs id that needs to be shown