将表单加载到 iframe 中

发布于 2024-10-07 01:17:34 字数 1893 浏览 5 评论 0原文

我想将表单加载到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-10-14 01:17:34

您可能想尝试使用 loginIFrame.contentWindow.document 而不是 loginIFrame.appendChild 添加表单。

您可能还需要在 iframe 添加到页面后添加表单,因为 contentWindow 属性不可用。

我可能是错的,但我很确定表单没有被添加到 iframe 中,因为您正在使用 appendChild。我真的不认为你可以操纵它,因为它将加载你告诉它加载的 URL(即 about:blank)。

编辑:您可能还想添加 loginForm.setAttribute("target", "loginFrame");

这是我一直在测试的内容,它工作正常:

loginIFrame = document.createElement('iframe');
loginIFrame.src = "about:blank";
loginIFrame.style.top = "0px";
loginIFrame.style.position='fixed';
loginIFrame.style.display = 'block';
loginIFrame.style.zIndex = 100;
loginIFrame.style.border = "solid #48D236 10px";
loginIFrame.height = "100px";
loginIFrame.width = "100%";
loginIFrame.style.border = 0;
loginIFrame.id = "loginFrame";
loginIFrame.name = "loginFrame";

document.body.appendChild(loginIFrame);

var idocument = loginIFrame.contentWindow.document;

loginForm = idocument.createElement("form");
loginForm.setAttribute("target", "loginFrame");
loginForm.setAttribute("method","Post");
loginForm.setAttribute("action","http://devserver:8000/action/");
parameters = {};
parameters['url'] = parent.window.location;

for(var key in parameters)
{
    var hiddenField = idocument.createElement("input");
    hiddenField.setAttribute('type',"hidden");
    hiddenField.setAttribute('name',key);
    hiddenField.setAttribute('value',parameters[key]);
    loginForm.appendChild(hiddenField);
}

loginIFrame.appendChild(loginForm);

loginForm.submit();
  • Christian

You might want to try adding the form using loginIFrame.contentWindow.document instead of loginIFrame.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:

loginIFrame = document.createElement('iframe');
loginIFrame.src = "about:blank";
loginIFrame.style.top = "0px";
loginIFrame.style.position='fixed';
loginIFrame.style.display = 'block';
loginIFrame.style.zIndex = 100;
loginIFrame.style.border = "solid #48D236 10px";
loginIFrame.height = "100px";
loginIFrame.width = "100%";
loginIFrame.style.border = 0;
loginIFrame.id = "loginFrame";
loginIFrame.name = "loginFrame";

document.body.appendChild(loginIFrame);

var idocument = loginIFrame.contentWindow.document;

loginForm = idocument.createElement("form");
loginForm.setAttribute("target", "loginFrame");
loginForm.setAttribute("method","Post");
loginForm.setAttribute("action","http://devserver:8000/action/");
parameters = {};
parameters['url'] = parent.window.location;

for(var key in parameters)
{
    var hiddenField = idocument.createElement("input");
    hiddenField.setAttribute('type',"hidden");
    hiddenField.setAttribute('name',key);
    hiddenField.setAttribute('value',parameters[key]);
    loginForm.appendChild(hiddenField);
}

loginIFrame.appendChild(loginForm);

loginForm.submit();
  • Christian
风追烟花雨 2024-10-14 01:17:34

可能将表单目标设置为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文