admin管理员组

文章数量:1303647

I want to relocate on button click to a relative url:

<button onclick="document.location.href='/Recruiters.aspx'"></button>

but this is not working...

Any suggestions?

I want to relocate on button click to a relative url:

<button onclick="document.location.href='/Recruiters.aspx'"></button>

but this is not working...

Any suggestions?

Share Improve this question asked Apr 15, 2013 at 13:13 David Van StadenDavid Van Staden 1,7899 gold badges35 silver badges52 bronze badges 4
  • 1 Try with window.location.href. – Brugnar Commented Apr 15, 2013 at 13:17
  • Is the button inside a form? – Esailija Commented Apr 15, 2013 at 13:18
  • @Brugnar...nope does not work – David Van Staden Commented Apr 15, 2013 at 13:26
  • @DavidVanStaden window.location.href wouldn't fix it, but it's preferred over document.location.href – Ian Commented Apr 15, 2013 at 13:30
Add a ment  | 

2 Answers 2

Reset to default 6

The default type of a button is "submit" so when inside a form, clicking the button submits it. Though it should first redirect but it's worth a shot:

<button type="button" onclick="document.location.href='/Recruiters.aspx'"></button>

Your onclick needs to be a handler. You could do it like this:

<script>
    function redirect()
    {
        window.location.href='/Recruiters.aspx';
    }
</script>

<button onclick="redirect()"></button>

If you don't want to use a handler you can still do it inline like this:

<button onclick="javascript: window.location.href='/Recruiters.aspx';"></button>

本文标签: javascriptdocumentlocationhref for relative pathStack Overflow