Andrew
Andrew

Reputation: 3589

Javascript URL redirect

I'm creating a mock up version of google censorship for history class. I'm trying to modify the redirect url so I can put my own terms into the 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"
}

in the else block, I try to insert keyTerms into the URL, but I end up with undefined when the search begins. Any suggestions? Here is the URL for the website: http://andrewgu12.kodingen.com/history/

Upvotes: 0

Views: 1655

Answers (2)

theelfismike
theelfismike

Reputation: 1621

In the header bar at the top, you have a button labeled "Search" that also has the id "search". Try removing the id from that element.

Upvotes: 0

Andreas Wong
Andreas Wong

Reputation: 60594

The "Search" link on the top bar of the same page has the same id of search, thus when you do your document.getElementById('search'), javascript returns the first element in the DOM with id = search, which is the anchor on the top bar instead of your input.

You can instead give a name attribute to your search form:

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

And in your js

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

Upvotes: 1

Related Questions