将meta标签附加到来自iframe的主文档的头部 [英] append meta tag to the head of the main document from iframe

查看:1432
本文介绍了将meta标签附加到来自iframe的主文档的头部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一个问题,我已经将文档作为响应,但它显示在iframe中,其中父文档具有错误的元标记。

我建立了一个测试,看看我是否可以在iframe中完成它。它将元标记附加到iframe的头部而不是主文档。任何人有任何建议?



index.html

 < html xmlns =http://www.w3.org/1999/xhtmlxml:lang =enlang =en> 
< head>
< title> Iframe Test< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = iso-8859-1>

< / head>
< body>

< iframe id =testFramename =testFramesrc =iframe.html>< / iframe>

< / body>
< / html>



iframe.html

 < html xmlns =http://www.w3.org/1999/xhtmlxml:lang =enlang =en> 
< head>
< title> Iframe< / title>

< script>
var viewPortTag = document.createElement('meta');
viewPortTag.id =viewport;
viewPortTag.name =viewport;
viewPortTag.content =width = device-width; initial-scale = 1.0; maximum-scale = 1.0; user-scalable = 0;;
document.getElementsByTagName('head')[0] .appendChild(viewPortTag);

< / script>
< / head>
< body>
Iframe原始内容
< / body>
< / html>


解决方案

您可以做到这一点,但只有iframe与顶级文档的域相同。



只需将您的代码更改为:

  window.parent.document.getElementsByTagName( '头')[0] .appendChild(viewPortTag); 


So I've got an issue where i've made a document responsive but it's displayed in an iframe which where the parent document has the wrong meta tag.

I set up a test to see if i could do it in an iframe. It's appending the meta tag to the head of the iframe not the main document. Anyone have any suggestions?

index.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Iframe Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>

<iframe id="testFrame" name="testFrame" src="iframe.html"></iframe>

</body>
</html>

iframe.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Iframe</title>

<script>
var viewPortTag=document.createElement('meta');
viewPortTag.id="viewport";
viewPortTag.name = "viewport";
viewPortTag.content = "width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;";
document.getElementsByTagName('head')[0].appendChild(viewPortTag);

</script>
</head>
<body>
Iframe Original Content
</body>
</html>

解决方案

You can do it, but only if the domain of the iframe is the same of the top document's domain.

Just change your code to:

window.parent.document.getElementsByTagName('head')[0].appendChild(viewPortTag);

这篇关于将meta标签附加到来自iframe的主文档的头部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆