views:

53

answers:

4
+6  Q: 

Page Redirection

I'm working on a script where all I want it to do (right now) is redirect the user based on which button they press. Eventually it will take form input and incorporate that into the redirect, but right now I'm just trying to get the buttons to send the user off to the appropriate site. However, My redirects aren't working.

<html>
<head>
<title>
Home
</title>
</head>

<body>

<script type="text/javascript">
<!--

var textstring;
var btnWhichButton;

//Gets the text from the form
function getQ() {
    textstring = document.forms['Search'].elements[0].value;
}

//Does a Google Search
function googleSearch() {
    window.location ="http://www.google.com";
}

//Does a YouTube Search
function youtubeSearch() {
    window.location = "http://youtube.com"; 
}

//Figure out which button was pressed
function whichButton() {
    if (btnWhichButton.value == 'Google Search' ) {
        googleSearch();
    } else if (btnWhichButton.value == 'YouTube Search' ){
        youtubeSearch();
    } 
}

//main function to run everything
function main() {
    getQ();
    whichButton();
}
// -->
</script>


<form name="Search" >

<input type="text"   name="q" size="31" maxlength="255" value="" />
<input type="submit" value="Google Search" onclick="btnWhichButton=this; main();" />
<input type="submit" value="YouTube Search" onclick="btnWhichButton=this; main();" />

</form> 
</body>

</html>

When either button is clicked, the page just reloads with ?q= appended to the url, it doesn't redirect. Any help?

+1  A: 

You want to use a button not an input type='submit'. Your current buttons are submitting the form, not performing their onclick actions.

Or block the submit action in some way. Or you could use your functions to set the form action to the url and just let it submit.

jasonbar
Thank you. This answered my question.
Matt
A: 

Your scripts seem highly overcomplicated. Why not have three functions: getQ, googleSearch, and youTubeSearch? Then inside the onClick event you can call the exact function, including this.value inside the input parameters and calling getQ from inside that function? Your method seems highly inefficient. If you're going to have separate functions for each of them anyways, there's no use in going through two other functions in order to get to them.

A submit button will always submit the form without a return false at the end of the onClick event, and since the default posting method is GET, its attaching ?q= to the end of your URL because that field is blank and it's the only input field in the form.

animuson
A: 

For redirecting to new page you no need to use the big javascript function.

<html> <body>
    <input type="button" value="Google Search" onclick="javascript:window.location.href='http://www.google.com'" />
    <input type="button" value="You tube Search" onclick="javascript:window.location.href='http://youtube.com'" />
</body></html>

Please check whether it helps you.

Hojo
A: 

Well as jasonbar says, change your input to be of type 'button' and not 'submit'. Plus, I'd rather use window.location.href instead of window.location only. I don't know possible this is good practice...happy programming.

deostroll