JavaScript 自动创建 iframe
JavaScript Automated Iframe Creation
我需要知道如何根据目录中的html文件数量自动创建Iframe。假设我在main.html
所在位置的子目录中1.html
和2.html
。我想自动创建 2 个 Iframe(main.html
个),一个显示1.html
,另一个显示2.html
。顺便说一句,如果你不知道,2.html
和1.html
是本地文件
尝试像这样实现:
$.ajax({
url: "/images-folder-on-server/",
success: function (data) {
var file_count = $(data).length();
$.each( data, function( key, value ) {
appendIframe(value);
});
}
});
function appendIframe( file)
{
var iframe = document.createElement('iframe');
iframe.src = file;
iframe.style.width = "100px";
iframe.style.height = "100px";
document.body.appendChild(iframe);
}
对服务器进行 ajax 调用以获取目录中的文件列表
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(){
//iterate the list and call addIframe( src ) for each of them
};
httpRequest.open('GET', "listFilesInDirectory");
httpRequest.send();
这就是addIframe的样子
function addIframe( src )
{
var iframe = document.createElement('iframe');
iframe.src = src;
iframe.style.width = "640px";
iframe.style.height = "px";
document.body.appendChild(iframe);
}
addIframe( "subdir/1.html" );
addIframe( "subdir/2.html" );
我认为
问题不在于如何使用Javascript创建iFrame,而在于计算使用它的文件。我建议创建对服务器端语言的ajax请求来计算文件的数量,然后您可以使用该数字使用jQuery创建iFrame元素。您可以使用以下用PHP
编写的代码作为服务器端脚本:-
<?php
function getNoFiles(){
$i = 0;
$dir = 'yourHTMLDirectory/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
echo $i;
}
?>
您可以在简单循环下使用这样的jQuery简单代码进行创建,限制从PHP代码中获取计数。
$('<your iframe tag></iframe>').appendTo('your div or area class or id');
相关文章:
- 使用jQuery从原始页面内容创建iframe
- 动态创建的iframe中的window.onerror在lne 0处给出脚本错误
- 谷歌索引iframe动态创建了什么
- 如何在不使用iframe或库的情况下创建剪切显示窗口区域
- 动态(重新)创建iframe内容不会重置Chrome中的JS对象
- 附加'沙箱'属性设置为动态创建的iframe以停止重定向
- 如何设置外部javascript创建的iframe的位置
- 如何在 iframe 中创建 HTML5 数据属性
- 创建使用 ajax 更新 iframe 的链接
- 如何在跨域中创建响应式iframe
- 如何创建iframe内容的书签
- 按日期为iframe创建一个HTML链接
- 如何从主框架获取对在iframe上创建的对象的引用
- 在Internet Explorer 8中的Iframe中创建TinyMCE时出现问题
- 为其他域的 iframe 创建内容
- Ajax Iframe 创建(Spotify widget):x 帧错误
- 使用 jquery 从 iframe 创建弹出窗口
- 为iFrame创建浮动水平滚动条
- ExtJs,从不同版本的iframe创建窗口,正确设置高度
- 为两个iframe创建公共卷轴