2012-11-06 18 views
5
<html> 
<head> 
<script> 
function open_win() 
{ 
    window.open("http://localhost:8080/login","mywindow") 
} 
</script> 
</head> 
<body> 

<input type="button" value="Open Window" onclick="open_win()"> 

</body> 
</html> 

喜發送自定義參數,的javascript:window.open與(),但它不工作

上的一個按鈕,我打開一個new website(我的網站)的點擊 我有兩個text fields(一文本字段和另一個密碼字段),我試圖發送這個值到另一個打開的窗口。

但它不工作,因爲我想。

我曾嘗試以下方法

1. window.open("http://localhost:8080/login?cid='username'&pwd='password'","mywindow") 

2. window.open("http://localhost:8080/login","mywindow") 
    mywindow.getElementById('cid').value='MyUsername' 
    mywindow.getElementById('pwd').value='mypassword' 

有誰請幫助我,如果這是可以或不可以?

對不完整的細節,它的後發送請求。

+7

你知道通過密碼字段傳遞到'window.open'是**危險**,因爲它是未加密的並且可以讓所有人都清楚地看到? –

回答

8

,如果你想通過POST變量,你必須使用一個HTML表單:

<form action="http://localhost:8080/login" method="POST" target="_blank"> 
    <input type="text" name="cid" /> 
    <input type="password" name="pwd" /> 
    <input type="submit" value="open" /> 
</form> 

或:

如果你想傳遞一個URL GET變量,他們亂寫單引號

http://yourdomain.com/login?cid=username&pwd=password 

這裏是如何創建上面javascrpt變量的字符串:

myu = document.getElementById('cid').value; 
myp = document.getElementById('pwd').value; 
window.open("http://localhost:8080/login?cid="+ myu +"&pwd="+ myp ,"MyTargetWindowName"); 

在具有該URL的文檔中,您必須讀取GET參數。如果它在php中,請使用:

$_GET['username'] 

請注意:傳輸密碼的方式是一個很大的安全漏洞!

+0

GET變量來自JavaScript變量* – Quentin

1

要連接字符串,請使用+運算符。

要將數據插入到URI中,請將其編碼爲URI。

壞:

​​

好:

var url_safe_username = encodeURIComponent(username); 
var url_safe_password = encodeURIComponent(password); 
var url = "http://localhost:8080/login?cid=" + url_safe_username + "&pwd=" + url_safe_password; 

服務器將必須處理的查詢字符串,使數據的使用。你不能指定任意的表單域。

...但不要觸發新的窗口或在URI中傳遞證書(他們暴露在肩膀上的攻擊可能會被記錄)。

1

你可以用這一點,但仍然存在一個安全問題

<script type="text/javascript"> 

function fnc1() 
{ 
    var a=window.location.href; 

    username="p"; 
    password=1234; 
    window.open(a+'?username='+username+'&password='+password,""); 

} 

</script> 
<input type="button" onclick="fnc1()" /> 
<input type="text" id="atext" /> 
0
You can try this instead 


var myu = document.getElementById('myu').value; 
var myp = document.getElementById('myp').value; 
window.opener.location.href='myurl.php?myu='+ myu +'&myp='+ myp; 

注意:不要使用此方法來傳遞敏感信息像用戶名,密碼。

0

請找到該示例代碼,您可以使用隱藏的表單,以便用POST數據發送到您的網址,如下所示:

function open_win() 
{ 
    var ChatWindow_Height = 650; 
    var ChatWindow_Width = 570; 

    window.open("Live Chat", "chat", "height=" + ChatWindow_Height + ", width = " + ChatWindow_Width); 

    //Hidden Form 
    var form = document.createElement("form"); 
    form.setAttribute("method", "post"); 
    form.setAttribute("action", "http://localhost:8080/login"); 
    form.setAttribute("target", "chat"); 

    //Hidden Field 
    var hiddenField1 = document.createElement("input"); 
    var hiddenField2 = document.createElement("input"); 

    //Login ID 
    hiddenField1.setAttribute("type", "hidden"); 
    hiddenField1.setAttribute("id", "login"); 
    hiddenField1.setAttribute("name", "login"); 
    hiddenField1.setAttribute("value", "PreethiJain005"); 

    //Password 
    hiddenField2.setAttribute("type", "hidden"); 
    hiddenField2.setAttribute("id", "pass"); 
    hiddenField2.setAttribute("name", "pass"); 
    hiddenField2.setAttribute("value", "[email protected]$"); 

    form.appendChild(hiddenField1); 
    form.appendChild(hiddenField2); 

    document.body.appendChild(form); 
    form.submit(); 

} 
相關問題