IFRAME adaptive Height: same domain/Cross-Domain mvc3

Source: Internet
Author: User
Tags website domain names

IFRAME adaptive Height: same domain/Cross-Domain


IFRAME self-adaptive height was used in the recent project. At that time, Baidu searched for it, and it was really difficult to grasp it, however, when I found the code in my hand, as long as I visited the DOM attribute of my subordinate website, I reported the error "no permission, Access Denied". The first error was big. Later I found that, it turned out to be an HTTP security factor. Cross-origin access is not allowed to access the attributes of other websites. Think about the attributes. Otherwise, no one can access other websites. Now we have sorted out the solutions for online collection.

1: same domain

In the same domain name, the security factor is not a problem. You are allowed to access your own Dom attributes. Directly paste the online code:

<IFRAME id = "iframepage" name = "iframepage" frameborder = "0" scrolling = "no" width = "100%" src = "/controller/Action" onLoad = "setcwinheight (this) ">
</Iframe>

View code

 1 function SetCwinHeight(iframeObj) { 2             if (document.getElementById) { 3                 if (iframeObj) { 4                     this.height = iframepage.document.body.scrollHeight; 5                     if (iframeObj.contentDocument && iframeObj.contentDocument.body.offsetHeight) { 6                         iframeObj.height = iframeObj.contentDocument.body.offsetHeight + 20; 7                     } else if (document.frames[iframeObj.name].document && document.frames[iframeObj.name].document.body.scrollHeight) { 8                         iframeObj.height = document.frames[iframeObj.name].document.body.scrollHeight + 20; 9                     }10                 }11             }12         }

There are many other codes with the same-domain adaptive height on the internet...

2: Cross-Origin

Thank you for your blog on the Internet. Otherwise, I am still looking for a visit... The following is my learning experience.

Implementation principle: for example, if there are two website domain names a.com; B .com; website a has an IFRAME, you need to put website B in the IFRAME, however, the IFRAME height must first obtain the page height of website B, and then modify the IFRAME height value of website a Based on the obtained height. 1: To obtain the height of your own page on website B (that is, the subordinate website). 2: website B does not have the permission to modify the height of website a. An intermediate page is required, the height of website A can be modified on this intermediate page. the intermediate page must be in the same domain as website.

Key code of website:

<IFRAME id = "iframepage" name = "iframepage" frameborder = "0" scrolling = "no" width = "100%" src = "@ viewdata [" urlstr "]">
</Iframe> // SRC is the page address of website B.

Key code of website B:

Add an IFRAME under website B to direct the intermediate page of website a to get rid of the IFRAME height of website.

<! Doctype HTML>
<HTML>
<Head>

<SCRIPT type = "text/JavaScript">

$ (Function (){

Hashh = document.doc umentelement. scrollheight;
Urlc = "http://www.joantest.com: 9001/home/agent"; // for the intermediate page of a domain name, here must use domain name, cannot use IP
Document. getelementbyid ("middleiframe"). src = urlc + "#" + hashh;
});

</SCRIPT>

</Head>
<Body>
<Div class = "toptitle">
.....
</Div>
@ Renderbody ()
<IFRAME id ="Middleiframe"Name ="Middleiframe"Src =" http ://Www.joantest.com: 9001/home/agent "width =" 0 "Height =" 0 "style =" display: none; ">
</Iframe>
</Body>
</Html>

Key code of the intermediate page:

This page is a file under Domain A. The SRC above shows that I opened an agent method under homecontroller of website.

<SCRIPT type = "text/JavaScript">
$ (Function (){
VaR iobj = parent.parent.doc ument. getelementbyid ('iframepage'); // locate the IFRAME in the field and modify its value
Iobloud = parent. Parent. Frames ["iframepage"]. Frames ["middleiframe"]. Location. Hash;
Iobj. style. Height = iobloud. Split ("#") [1] + "PX ";
});
</SCRIPT>

 

In this way, it has been modified, butIt is the intermediate page of domain name a. The domain name must be used here, and IP addresses are not available"The SRC in this area is generally used to access the website through IP addresses in the same network segment of the common LAN. Therefore, a domain name ing is required for this area. The method is as follows:

Open the hosts file in the "C: \ windows \ system32 \ drivers \ etc" Path

Add the Domain Name of the URL to be mapped: # comment

 

 

 

 

 

 

 

 

 

 

 

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.