Implementation of high adaptive cross-domain in IFRAME

Source: Internet
Author: User

The implementation of this effect requires that both the parent page and the child page need to be set accordingly
The following are the specific settings and code

First, the parent layer page settings

1, is the parent layer of the IFRAME introduced

The code is as follows Copy Code

<iframe frameborder= "0" scrolling= "no" width= "100%" src= "iframe.html" id= "Paopaowang" ></iframe>


2, the parent layer to design a domain name variables domain

The code is as follows Copy Code

<script type= "Text/javascript" >
Document.domain = "aqy106.com";
</script>

Ii. setting of the contained IFRAME

In this page, you need to give your body or use a container to wrap all the elements in the IFRAME, and then add IDs

1.id= "Bodybox"
Then add the following script code, which should be noted is Document.domain = "aqy106.com"; The domain name here to set sing Woo the same domain name as the parent layer

The code is as follows Copy Code

<script type= "Text/javascript" >
Document.domain = "aqy106.com";
function Resetheight (h, SCL) {
if (!top.document.getelementbyid ("Paopaowang")) return;
With (Top.document.getElementById ("Paopaowang")) {
if (offsetheight!= h) {
Style.height = h + ' px ';
}
}
Top.document.body.style.height = Top.document.body.scrollHeight;
if (!SCL) {
if (top.document.documentElement.scrollTop) top.document.documentElement.scrollTop = 0;
if (top.document.body.scrollTop) top.document.body.scrollTop = 0;
}
H=document.getelementbyid ("Bodybox"). offsetheight;
With (Top.document.getElementById ("Paopaowang")) {
if (offsetheight!= h) {
Style.height = h + ' px ';
}
}
}
Window.onload=function () {
Resetheight (document.getElementById ("Bodybox"). offsetheight);
}
</script>

This has been completed, the specific code is very simple and clear, you can modify and analyze, you can specifically try to put the code in the same domain or different fields to try.
But this script also has the imperfect place, is if the IFRAME page has the script to carry on the operation, has changed the IFRAME page the height, the parent layer's page iframe height cannot automatically adapt, also can only realize the browser initial loading can obtain the IFRAME the height and the loading altitude successfully, But in the firame operation when the parent layer can not adapt to the height of real-time, so there are improvements in place, will continue to improve.


other methods provided by Webmaster

Key code:

IFRAME Homepage: main.html

The code is as follows Copy Code

<iframe id= "Iframeb" name= "Iframeb" src= "www.b.com/B.html" width= "100%" height= "Auto" scrolling= "no" frameborder= " 0 "></iframe>

IFrame Nesting page: b.html

The code is as follows Copy Code

<iframe id= "Iframea" name= "Iframea" src= "" width= "0" height= "0" style= "display:none;" ></iframe>
<script type= "Text/javascript" >
function Sethash () {
Hashh = Document.documentElement.scrollHeight; Get your own height
URLC = "www.a.com/A.html"; Set SRC for Iframea
document.getElementById ("Iframea"). src=urlc+ "#" +HASHH; Passing the height as a parameter
}
Window.onload=sethash;
</script>

Intermediary page: a.html

The code is as follows Copy Code

<script>
function Pseth () {
var iobj = parent.parent.document.getElementById (' Iframeb ');//a and main domain, so you can access nodes
IOBJH = parent.parent.frames["Iframeb"].frames["Iframea"].location.hash;//access its own location object to get the hash value
IObj.style.height = Iobjh.split ("#") [1]+ "px";//Operation Dom
}
Pseth ();
</script>

Cross-domain methods

Realization principle: For instance now has two website domain name is a.com respectively;  B.Com;  A site has an IFRAME, need to put B site to the IFRAME, but the height of the iframe to get B site page height, and then according to the height of the gain to modify a site iframe height value. 1: is to be in the B site (that is, subordinate sites) to get their own height of the page, 2:b site does not have the right to modify the height of a site, need a middle page, the middle page can modify the height of a site, so. This intermediate page must be under the same domain as a site

A site's key code:

The code is as follows Copy Code

<iframe id= "Iframepage" name= "iframepage" frameborder= "0" scrolling= "no" width= "100%" src= "@ViewData [" Urlstr "]" >
</iframe>//SRC is the page address of the B website

b site's key code:

In the B site also add an IFRAME, the IFRAME is to guide a site of the middle page, thereby getting rid of a site of the iframe height.

The code is as follows Copy Code

<! DOCTYPE html>

<script type= "Text/javascript" >

$ (function () {

Hashh = Document.documentElement.scrollHeight;
URLC = "Http://www.:9001/Home/Agent"; A domain name for the middle page, here must use the domain name, can not use IP
document.getElementById ("Middleiframe"). src=urlc+ "#" +HASHH;
});

</script>

<body>
<div class= "Toptitle" >
.....
</div>
@RenderBody ()
<iframe id= "Middleiframe" name= "Middleiframe" src= "http://www.:9001/Home/Agent" width= "0" height= "0" style= " Display:none; " >
</iframe>
</body>

Key code for the middle page:

The page is in a domain name under a file, from the above SRC know I was in a site HomeController under the Open agent method

  code is as follows copy code

<script Type= "Text/javascript" >  
    $ (function () {
              var iobj = parent.parent.document.getElementById (' iframepage '); Locate the IFRAME under the domain and then modify its value
        iobjh = parent.parent.frames["Iframepage"]. frames["Middleiframe"].location.hash;
        iObj.style.height = Iobjh.split ("#") [1] + "px";
   });    
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.