简体   繁体   English

是否可以“伪造”iframe的src属性?

[英]Is it possible to “fake” the src attribute of an iframe?

I try to add dynamically an iframe to a web page using JavaScript. 我尝试使用JavaScript动态地将iframe添加到网页中。 I would like to know if that's possible to set the src attribute of my iframe without using another html file via an URL. 我想知道是否可以通过URL使用另一个html文件来设置我的iframe的src属性。 I mean is there a way to "fake" the html for the src attribute file with a JS variable where we I can set my code (which is JS itself) ? 我的意思是有没有办法用JS变量“伪造”src属性文件的html,我可以设置我的代码(JS本身)? I would use the DOM createElement to create the iframe in jQuery. 我会使用DOM createElement在jQuery中创建iframe。

Thanks ! 谢谢 !

You could look into data: URIs . 您可以查看data: URI

<iframe src="data:text/html, .... URLencoded HTML data ....">

alternatively 或者

<iframe src="data:text/html;base64, .... base64 encoded HTML data ....">

The scheme is supported by IE >= 8 (MSDN source) , Firefox, Safari 3+ and Opera. 该方案由IE> = 8 (MSDN源) ,Firefox,Safari 3+和Opera支持。

It has differing length limits. 它有不同的长度限制。 Opera is said to cut off at about 4 kilobytes; 据说Opera的切断量约为4千字节; Internet Explorer at 32 kilobytes. Internet Explorer为32千字节。 Firefox has no explicit length limit. Firefox没有明确的长度限制。

More on data URIs and tools for converting at Mozilla Developer Central 有关在Mozilla Developer Central进行转换的数据URI和工具的更多信息

如果您完全控制iframe客户端,并且永远不需要使用该iframe进行服务器端请求,那么将div设置为iframe样式(查看overflow属性)可能会更容易,您将拥有对该div的DOM内容进行更简单,更直接的控制。

following code should do what you want. 以下代码应该做你想要的。

you can leave the src attribute empty. 你可以将src属性留空。

var yourcontent="<div>your stuff</div>";
window.frames['frame_name'].document.write=yourcontent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM