0

I have a JSF button which calls a Javascript function when it is pressed, that function is doing some work and at the end it redirects to another page, it looks like this:

<h:commandButton type="submit" value="Submit" id="button1" onclick="myFunction()">
</h:commandButton>

And the function looks like this:

function myFunction() {
    // .....
    window.location.href = "/page2.faces";
}

The HTML code is generated fine when the page is opened:

<input type="submit" value="Submit" id="form1:button1" name="form1:button1" onclick="myFunction()" />

The function is executed but the redirection does not work, it remains on the same page. Is there a way to redirect from a Javascript function?

-------UPDATE--------

The form tag is generated as follows:

<form id="form1" name="form1" method="post" action="/myProject/page1.faces" enctype="application/x-www-form-urlencoded" class="form">

The action is the same as the first page, seems like when I click the button it navigates to the same page because of this.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
CIOC
  • 1,385
  • 3
  • 19
  • 48
  • Should work. Any errors on the browser developer tools console? – Johannes Jander Feb 10 '16 at 16:11
  • There are no errors, it just stays on the same page – CIOC Feb 10 '16 at 16:48
  • Something really strange is happening, if I click on the button it stays on the same page but if I set a breakpoint and execute the function step by step using the Browser dev tools at the end it successfully redirects to the other page. – CIOC Feb 10 '16 at 16:50
  • Bad usage of an `h:commandButton`. See http://stackoverflow.com/questions/13070537/difference-between-hbutton-and-hcommandbutton – Kukeltje Feb 10 '16 at 17:52
  • Sorry, I should have clarified that, I'm using JSF 1.2 and seems like the `` tag only is supported in JSF 2.x – CIOC Feb 10 '16 at 18:06
  • what happens if you throw out the `action` parameter of the form? – Johannes Jander Feb 10 '16 at 20:40
  • The `action` parameter is automatically generated by the JSF runtime and it always points to the same page, if I remove the `form` tag completely then it redirects to the other page, but I need the form because some components require a form to work properly. – CIOC Feb 10 '16 at 21:28

0 Answers0