6

I want to make a button that will redirect me to another page.

the code:

 <div align="right" ><input type="submit" id="logout" onclick="location.href=/login?dis=yes" value="Sign Out" ></div><BR> 

but when i press the button, i dont get any redirection to the other page.

any idea why?

7 Answers 7

10

Your current code, even if the syntax is fixed by adding quotation marks, creates a button that does not work when JavaScript is disabled. The simpler and safer approach is a small HTML form:

<form action="/login"> <input type=hidden name=dis value=yes> <input type=submit value="Sign Out"> </form> 

To achieve the desired styling, you could add the following stylesheet:

form { display: inline; float: right; } 
Sign up to request clarification or add additional context in comments.

Comments

7

Perhaps try this using a type="button" and put quotes around the location.

<div align="right"><input type="button" id="logout" onclick="location.href='/login?dis=yes'" value="Sign Out" ></div> 

Comments

2

don't use

<input type="submit" /> 

because this is used to post the form, instead you should use

<input type="button" onclick='location.href="/login?dis=yes"'/> 

for redirection or plain anchor tag

<a href="/login?dis=yes">Sign Out</a> 

Comments

0

Your javascript code is invalid.

Use this:

<div align="right" ><input type="submit" id="logout" onclick="location.href='/login?dis=yes'" value="Sign Out" ></div><BR> 

Comments

0

try this

window.location='/login?dis=yes'; 

as

 <div align="right" > <input type="submit" id="logout" onclick="window.location='/login?dis=yes';" value="Sign Out" ></div> 

Comments

0
  • Set the target to Window.Location so you get the right target changed.
  • add return false; so that the submit button does not try to submit the form.
  • Put the URL in single quotes to avoid parsing errors.

<input type="submit" id="logout" onclick="window.location.href='/login?dis=yes';return false;" value="Sign Out">

The main reason that your button, as designed, will not work is because it is a "Submit" button. When you click on this it is trying to do the submit action after running your onclick action, essentially ignoring the onclick action.

You can fix this by using a type="button" button, or the above solution I listed if you must use a "submit" button.

Comments

0

I would tell you the same thing everyone else has. Instead of using: location.herf you should use: window.location Hope this helps you!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.