将表单加载到 iframe 中
我想将表单加载到 iframe 中。当用户单击书签时,iframe 将加载到随机页面中。
这是到目前为止的代码:
loginForm = document.createElement("form");
loginForm.setAttribute("method","Post");
loginForm.setAttribute("action","http://devserver:8000/action/");
parameters = {};
parameters['url'] = parent.window.location;
for(var key in parameters)
{
var hiddenField = document.createElement("input");
hiddenField.setAttribute('type',"hidden");
hiddenField.setAttribute('name',key);
hiddenField.setAttribute('value',parameters[key]);
loginForm.appendChild(hiddenField);
}
loginIFrame = document.createElement('iframe');
loginIFrame.src = "about:blank";
loginIFrame.appendChild(loginForm);
loginIFrame.style.top = "0px";
loginIFrame.style.position='fixed';
loginIFrame.style.display = 'block';
loginIFrame.style.zIndex = 100;
loginIFrame.style.border = "solid #48D236 10px";
loginIFrame.height = "25px";
loginIFrame.width = "100%";
loginIFrame.style.border = 0;
loginIFrame.id = "loginFrame";
loginIFrame.name = "loginFrame";
usernameField = document.createElement("input");
usernameField.type = "text";
usernameField.size = 8;
usernameField.name = "usernameField";
usernameField.id = "usernameField";
passwordField = document.createElement("input");
passwordField.type = "password";
passwordField.size = 8;
passwordField.name = "passwordField";
passwordField.id = "passwordField";
submitButton.style.position='fixed';
submitButton.style.top = "60px";
submitButton.type = "button";
submitButton.value = "Submit";
submitButton.onclick = function(){loginUser();};*/
b.style.position="relative";
addToBody(loginIFrame);
loginForm.submit();
最终发生的是整个页面在提交(最后一行代码)而不是 iframe 上重新加载。有什么想法吗?
提前致谢
I want to load a form into an iframe. The iframe will be loaded into a random page when the user clicks the bookmarklet.
Here is the code so far:
loginForm = document.createElement("form");
loginForm.setAttribute("method","Post");
loginForm.setAttribute("action","http://devserver:8000/action/");
parameters = {};
parameters['url'] = parent.window.location;
for(var key in parameters)
{
var hiddenField = document.createElement("input");
hiddenField.setAttribute('type',"hidden");
hiddenField.setAttribute('name',key);
hiddenField.setAttribute('value',parameters[key]);
loginForm.appendChild(hiddenField);
}
loginIFrame = document.createElement('iframe');
loginIFrame.src = "about:blank";
loginIFrame.appendChild(loginForm);
loginIFrame.style.top = "0px";
loginIFrame.style.position='fixed';
loginIFrame.style.display = 'block';
loginIFrame.style.zIndex = 100;
loginIFrame.style.border = "solid #48D236 10px";
loginIFrame.height = "25px";
loginIFrame.width = "100%";
loginIFrame.style.border = 0;
loginIFrame.id = "loginFrame";
loginIFrame.name = "loginFrame";
usernameField = document.createElement("input");
usernameField.type = "text";
usernameField.size = 8;
usernameField.name = "usernameField";
usernameField.id = "usernameField";
passwordField = document.createElement("input");
passwordField.type = "password";
passwordField.size = 8;
passwordField.name = "passwordField";
passwordField.id = "passwordField";
submitButton.style.position='fixed';
submitButton.style.top = "60px";
submitButton.type = "button";
submitButton.value = "Submit";
submitButton.onclick = function(){loginUser();};*/
b.style.position="relative";
addToBody(loginIFrame);
loginForm.submit();
What ends up happening is the entire page gets reloaded on the submit (last line of code) rather than the iframe. Any ideas?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想尝试使用
loginIFrame.contentWindow.document
而不是loginIFrame.appendChild
添加表单。您可能还需要在 iframe 添加到页面后添加表单,因为
contentWindow
属性不可用。我可能是错的,但我很确定表单没有被添加到 iframe 中,因为您正在使用
appendChild
。我真的不认为你可以操纵它,因为它将加载你告诉它加载的 URL(即 about:blank)。编辑:您可能还想添加
loginForm.setAttribute("target", "loginFrame");
这是我一直在测试的内容,它工作正常:
You might want to try adding the form using
loginIFrame.contentWindow.document
instead ofloginIFrame.appendChild
.You might also need to add your form after the iframe has been added to the page since the
contentWindow
property won't be available.I could be wrong, but I'm pretty sure that form isn't being added to the iframe, because you are using
appendChild
. I really don't think you can manipulate it that wa since it will be loading the URL you tell it to load (i.e. about:blank).Edit: You might also want to add
loginForm.setAttribute("target", "loginFrame");
Here is what I've been testing with and it works fine:
可能将表单目标设置为 iFrame。我想象,由于您是在“父”页面上下文中创建元素,所以它使用主页作为目标(即使它是嵌入的)。
编辑
您也可以尝试在创建 iFrame 后调用 createElement 以保留上下文。
Possibly set the form target to the iFrame. I'm imagining, since you're creating elements within the "parent" page context, it's using the main page as a target (even though it's embeded).
EDIT
You could also try calling createElement from the iFrame after you've created it to keep context too.