2010-12-09 123 views
0

我想将表单加载到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中。有任何想法吗?

在此先感谢

回答

1

你可能想尝试加入使用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(); 
  • 基督教
+0

嗯,我改变了最后包含loginForm = loginIFrame.contentWindow.document.createElement(“form”);但它不会重定向到/使用Firefox提交。在它的铬上。在Firefox上它只是停留在.src我设置iframe。有任何想法吗? – Yoshi9143 2010-12-09 20:36:28

0

可能将窗体目标设置为iFrame。我想象中,因为你在“父”页面上下文中创建元素,所以它使用主页面作为目标(即使它被嵌入)。

编辑 您也可以尝试从iFrame调用createElement以创建它以保持上下文。