El dominio A.HTML pertenece a la página principal A: www.taobao.com
El dominio b al que pertenece la página de IFRAMED B.HTML: www.alimama.com, asumiendo la dirección: http://www.alimama.com/b.html
Darse cuenta del efecto :La página bajo el nombre de dominio A A.HTML está incrustada en la página B.html bajo el nombre de dominio B a través de un iframe. Dado que el ancho y la altura de B.HTML son impredecibles y cambiarán, se requiere el tamaño adaptativo del iframe en A.HTML.
La naturaleza del problema:El problema del acceso a iframe de dominio cruzado de JS, porque para controlar la altura y el ancho del iframe en a.html, primero debe leer el tamaño de B.html. A y B no pertenecen al mismo dominio. Por razones de seguridad, el navegador restringe el acceso de dominio cruzado a JS y no puede leer la altura y el ancho de B.HTML.
Solución:El proxy proxy página c.html pertenece al mismo dominio a y a.html. C.HTML es una página proxy intermedia proporcionada bajo el dominio A a. Suponga que la dirección de C.HTML es: www.taobao.com/c.html, que es responsable de leer los valores de ancho y altura en la ubicación.
El código es el siguiente:código A.HTML
Primero, B.html se introdujo a través de iframe en A.HTML
<iframe id = b_iframe altura = 0 ′ width = 0 ′ src = http: //www.alimama.com/b.html FrameBorDer = no border = 0px marginwidth = 0 ′ marginHeight = 0 ′ desplazamiento = no dignTransparencia = sí> </iframe>
Código B.HTML
<script type = text/javaScript>
var b_width = math.max (document.documentelement.clientwidth, document.body.clientwidth);
var b_height = math.max (document.documentelement.clientheight, document.body.clientHeight);
var c_iframe = document.getElementById (c_iframe);
c_iframe.src = c_iframe.src+#+b_width+|+b_height; //http://www.taobao.com/c.html#Width|Height
}
</script>
<
<iframe id = c_iframe Height = 0 ′ width = 0 ′ src = http: //www.taobao.com/c.html style = Display: Ninguno> </iframe>
Código C.HTML
<script type = text/javaScript>
var b_iframe = parent.parent.document.getElementById (b_iframe);
var hash_url = window.location.hash;
var hash_width = hash_url.split (#) [1] .split (|) [0]+px;
var hash_height = hash_url.split (#) [1] .split (|) [1]+px;
b_iframe.style.width = hash_width;
b_iframe.style.height = hash_height;
</script>
El iframe en a.html puede adaptarse al ancho y la altura de B.html.
Otros problemas de operación de dominio cruzado similares a JS también se pueden resolver de acuerdo con esta idea