2012-11-03 45 views
0

當我想重定向時,變量其中總是未定義。但是,例如,我想把這個變量放在alert();它顯示正確的數字。javascript變量始終未定義

代碼

var where = msg.txt; 
window.location = "/page.php?id=".where; //this redirects to /page.php?id=undefined 
alert(where); //it show correct number 

回答

1

在JavaScript中,.用於屬性訪問,而不是像PHP中的字符串連接。

使用+代替:

window.location = "/page.php?id=" + where; 
3

它應該是:

window.location = "/page.php?id=" + where; 

您有:

"/page.php?id=".where; 

它試圖獲取一個字符串的where屬性,例如尚未確定。