2012-04-18 190 views
0

我正在爲歷史課程創建谷歌審查的模擬版本。我試圖修改重定向URL,所以我可以把我自己的方式進入的網址:Javascript URL重定向

function searchCensor() 
{ 
var keyTerms = document.getElementById("search").value; 
if(keyTerms == "censorship") 
    window.location = "http://andrewgu12.kodingen.com/history/censor.php"; 
else 
    window.location = "https://www.google.com/#hl=en&output=search&sclient=psy-ab&q="+keyTerms+"&oq="+keyTerms+"&aq=f&aqi=g4&aql=&gs_l=hp.3..0l4.851563l851824l0l851964l3l3l0l0l0l0l171l359l1j2l3l0.&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=bb242dc4ab43ba95&biw=1920&bih=963" 
} 
在else塊

,我嘗試插入keyTerms到URL,但我最終未定義當搜索開始。有什麼建議麼?下面是該網站的網址:http://andrewgu12.kodingen.com/history/

+0

id必須是唯一的。您的網頁上有兩次相同的ID(搜索)。 – Paulpro 2012-04-18 03:33:52

回答

1

「搜索」鏈接在同一頁的頂部欄上有search相同的ID,因此,當你做你的document.getElementById('search'),JavaScript和id = search返回的第一個元素在DOM ,這是頂部欄上的anchor而不是您的輸入。

可以代替給予name屬性搜索表單:

<form id="searchBox" name="searchForm"> 
    <input type="text" id="search" name="search" size="85"><br>   
</form> 

而在你的js

var keyTerms = document.searchForm.search.value; 
// rest of your code 
0

在頂部標題欄,你有一個按鈕標記爲「搜索」那也有ID "search"。嘗試從該元素中刪除該ID。