David Van Staden
David Van Staden

Reputation: 1779

document.location.href for relative path

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?

Upvotes: 3

Views: 11804

Answers (2)

Esailija
Esailija

Reputation: 140228

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>

Upvotes: 6

Kenneth
Kenneth

Reputation: 28737

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>

Upvotes: 0

Related Questions