如何使用用户脚本将框架添加到现有框架集? [英] How to add frames to an existing frameset using userscript?
问题描述
目标页面具有简单的HTML设置:
The target page has a simple HTML setup:
<html>
<body>
<frameset id="mainset" rows="50%,50%">
...
</frameset>
</body>
</html>
,我想使用JavaScript添加框架.
and I'd like to add frames using JavaScript.
这不起作用:
var parent = document.getElementsByTagName("frameset")[0];
var child = document.createElement("frame");
child.src = "about:blank";
child.style = "background-color: red;";
parent.appendChild(child);
这确实有效,但是会创建一个iframe:
This does work, but it creates an iframe:
var parent = document.getElementsByTagName("body")[0];
var child = document.createElement("iframe");
child.src = "about:blank";
child.style = "background-color: red;";
parent.appendChild(child);
是否可以使用javascript将框架添加到框架集中?
Is there a way to add frames to the frameset using javascript?
对不起,如果您之前曾问过这个问题.我搜索并找到了很多解决类似问题的人的解决方案",但是这些似乎不适用于此处.
Sorry if this question has been asked before. I searched and found a lot of 'solutions' for people having similar problems, but those don't seem to apply here.
目标页面如下:
推荐答案
仅添加新的<frame>
是不够的.您还必须调整包含的<frameset>
的rows
或cols
属性.
It's not enough to add a new <frame>
. You must also adjust the rows
or cols
attribute of the containing <frameset>
.
此外,如果框架集具有id
,则 使用 ,而不是尝试通过标记名获取节点. (ID快速且唯一.)
Also, if the frameset has an id
, use it instead of trying to get the node by tagname. (Ids are fast and unique.)
这样的代码可以使用(暂时):
Code like this will work (for now):
您可以 在此jsBin上查看运行中的代码 .
You can see the code in action at this jsBin.
var parent = document.getElementById ("mainset");
var child = document.createElement ("frame");
child.style = "background-color: pink;";
parent.appendChild (child);
parent.rows = "30%,30%,30%"
var docRdyInt = null;
var frmCW = child.contentWindow;
if (frmCW) {
//-- Must wait for the new frame to be writable, esp in Firefox.
docRdyInt = setInterval (createFramedPage, 50);
}
else {
alert ("Oopsie! may be a rendering delay in some cases. Try code from console.");
}
function createFramedPage () {
if (frmCW.document.readyState == "complete") {
clearInterval (docRdyInt);
frmCW.document.body.innerHTML = '<p>Hello World!</p>';
}
}
这篇关于如何使用用户脚本将框架添加到现有框架集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!