2010-12-09 119 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以創建它以保持上下文。