Home Web Front-end JS Tutorial IFrame cross-domain highly adaptive implementation code_javascript skills

IFrame cross-domain highly adaptive implementation code_javascript skills

May 16, 2016 pm 05:50 PM
Adaptive

复制代码 代码如下:

var iframeids = ['memberIndexIframe','inquiryCenterIframe','everychinaBbsIframe'];
var iframehide = "yes";
function dynIframeSize(){
var dyniframe = [];
for(var i=0;iif(!document.getElementById) return false;
dyniframe[i] = document.getElementById(iframeids[i]);
if(dyniframe[i] && !window.opera){
dyniframe[i].style.display = "block";
if(dyniframe[i].contentDocument && dyniframe[i].contentDocument.body.offsetHeight){
dyniframe[i].height = dyniframe[i].contentDocument.body.offsetHeight;
}else if(dyniframe[i].Document && dyniframe[i].Document.body.scrollHeight){
dyniframe[i].height = dyniframe[i].Document.body.scrollHeight;
}
}
if((document.all || document.getElement) && iframeids == "no") {
var tempobj = document.all?document.all[iframeids[i]] : document.getElementById(iframeids[i]);
tempobj.style.display = "block";
}
}
}
if (window.addEventListener)
window.addEventListener("load", dynIframeSize, false)
else if (window.attachEvent)
window.attachEvent("onload", dynIframeSize)
else {
window.onload = dynIframeSize;
}

对于上面的代码直接保存一个文件iframe.js,然后引用即可

注意:这段代码对于跨域的iframe引用不太适用,需要特别定义其iframe高度

下面这个代码是跨域的iframe高度

项目网站中需要嵌入合作网站的页面,这就需要页面的自适应高度变化,并且是跨域的。在网上看了许多资料,加上自己的实践,终于实现了跨域的IFrame自适应高度。如下:

首先,我的页面a.html需要引入对方的b.html,在这里运用iframe方式实现页面的套用
a.html页面的主要代码如下:
复制代码 代码如下:





a.html页面只是用来引入对方页面的一个简单页面 这就不多做介绍。
然后,最好在a.html同一级目录下,创建一个agent.html页面,该页面用来得到对方通过js传递过来的参数,
根据高度参数来调整a.html中的a_iframe的高度,主要代码如下:
Js代码
复制代码 代码如下:



最后,对方的页面(b.html),让对方在b.html中加入以下javascript代码
复制代码 代码如下:




Compare the height of the visible area of ​​the webpage and the height of the full text of the webpage through Math.max(clientHeight,scrollHeight) to get the larger value,
iframe passes this height to my agent.html through src , so that the Iframe in a.html can adapt to the height of the other party's b.html. There is no problem when tested under IE, but other browsers have not been tested.
In the process of implementing this function, I found a very useful js for getting the page height on the Internet, which is also recorded and quoted here:
Copy code The code is as follows:

<script> <br>var strInfo=" "; <br>strInfo=" rnVisible area width of web page:" document.body .clientWidth; <br>strInfo =" rnThe height of the visible area of ​​the web page: " document.body.clientHeight; <br>strInfo =" The width of the visible area of ​​rn the web page: " document.body.offsetWidth " (including the width of the edge)" ; <br>strInfo =" rnThe height of the visible area of ​​the web page: " document.body.offsetHeight " (including the width of the edge)" ; <br>strInfo =" rnThe width of the full text of the web page: " document.body.scrollWidth; <br>strInfo = " rnHeight of the full text of the webpage:" document.body.scrollHeight; <br>strInfo =" rnThe height of the webpage being scrolled:" document.body.scrollTop; <br>strInfo =" rnThe left side of the webpage being scrolled:" document.body.scrollLeft; <br>strInfo =" rnThe main body of the web page:" window.screenTop; <br>strInfo =" rnThe main body of the web page:" window.screenLeft; <br>strInfo =" rnScreen resolution The height: " window.screen.height; <br>strInfo =" rnThe width of the screen resolution: " window.screen.width; <br>strInfo =" rnThe height of the available work area of ​​the screen: " window.screen.availHeight; <br>strInfo =" rnScreen available workspace width:" window.screen.availWidth; <br>window.confirm(strInfo); <br></script>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure content adaptive brightness on Windows 11 How to configure content adaptive brightness on Windows 11 Apr 14, 2023 pm 12:37 PM

Adaptive brightness is a feature on Windows 11 computers that adjusts the brightness level of your screen based on the content being displayed or lighting conditions. Since some users are still getting used to Windows 11's new interface, Adaptive Brightness can't be easily found, and some even say the Adaptive Brightness feature is missing on Windows 11, so this tutorial will clear it all up. For example, if you're watching a YouTube video and the video suddenly shows a dark scene, Adaptive Brightness will make the screen brighter and increase the contrast level. This is different from auto-brightness, which is a screen setting that allows your computer, smartphone, or device to adjust brightness levels based on ambient lighting. There is a special one in the front camera

How to implement adaptive image size using CSS Viewport units vmin and vw How to implement adaptive image size using CSS Viewport units vmin and vw Sep 13, 2023 am 08:18 AM

How to use CSSViewport units vmin and vw to implement adaptive image size. In web design, we often encounter situations where images need to adapt to the screen size. To achieve this goal, CSS provides a powerful unit - the viewport unit. Among them, vmin represents the percentage of the smaller side of the viewport width, and vw represents the percentage of the viewport width. Therefore, we can use these two units to achieve the effect of adaptive image size. The specifics will be introduced below

How to build an adaptive mobile interface with Vue? How to build an adaptive mobile interface with Vue? Jun 27, 2023 am 11:05 AM

With the popularity of mobile Internet, more and more websites and applications need to consider the mobile experience. As a popular front-end framework, Vue has responsive layout and adaptive capabilities, which can well help us build adaptive mobile interfaces. This article will introduce how to use Vue to build an adaptive mobile interface. Using rem instead of px as the unit and using px as the unit in the mobile interface may result in inconsistent display effects on different devices. Therefore, it is recommended to use rem instead of px as the unit. rem is relative

CSS Viewport: How to use vmax and vw to implement adaptive text width CSS Viewport: How to use vmax and vw to implement adaptive text width Sep 13, 2023 am 10:16 AM

CSSViewport: How to use vmax and vw to implement adaptive text width. With the popularity of mobile devices, responsive design has become an important concept in web design. Among them, adaptive text width to maintain consistent display effects under different screen sizes is an important technology. This article will introduce how to use CSSViewport units, especially vmax and vw units, to implement adaptive text width. In addition to theoretical explanations, we will also provide specific

Adaptive Server in PHP8.0 Adaptive Server in PHP8.0 May 14, 2023 pm 01:10 PM

On November 26, 2020, the PHP team officially released the PHP 8.0 version. Compared with the previous version, PHP 8.0 brings many new features and improvements. One of the features worthy of attention is the adaptive server. This article will introduce the concept of adaptive server in PHP8.0 and its advantages. In previous PHP versions, developers could use PHP's own server (such as PHP-FPM, Apache) to run their own code. However, the disadvantages of these servers

Can vue be adaptive? Can vue be adaptive? Dec 30, 2022 pm 03:25 PM

Vue can achieve self-adaptation. The methods to achieve self-adaptation are: 1. Install the "scale-box" component through the "npm install" or "yarn add" command, and use "scale-box" to achieve adaptive scaling; 2. Through Set the device pixel ratio to achieve self-adaptation; 3. Set the zoom attribute through JS to adjust the zoom ratio to achieve self-adaptation.

How to implement adaptive grid through CSS Flex layout How to implement adaptive grid through CSS Flex layout Sep 26, 2023 pm 12:45 PM

How to implement adaptive grid through CSSFlex elastic layout Introduction: In web design, grid layout is a very common layout method. It can divide the web page into a uniform grid and can be adaptive on screens of different sizes. Adjustment. CSSFlex elastic layout provides a simple and powerful way to implement adaptive grid layout. This article will introduce how to use CSSFlex elastic layout to create an adaptive grid and provide specific code examples. 1. Basic steps: Create HTML

How to achieve adaptive left and right sidebars through CSS Flex layout How to achieve adaptive left and right sidebars through CSS Flex layout Sep 26, 2023 am 10:57 AM

How to achieve adaptive blurbs for the left and right sidebars through CssFlex elastic layout: With the continuous development of web design, realizing adaptive layout of the page has become an important requirement. The CSSFlex elastic layout is a good way to solve this problem. This article will introduce how to implement the adaptive layout of the left and right sidebars through CssFlex elastic layout, and give detailed code examples. 1. Introduction to Flex Layout 1.1 Flexible Containers and Flexible Projects Flex layout uses

See all articles