I used a plain html button tag in my Visualforce page to call my javascript remote action. The html code looks like this:

<div class="button-group">
     <button class="primary" type="button" ng-click="assignToFirm();">Assign Jobs</button>
     <button class="secondary" type="button"><a href="/{!order.Id}">Cancel</a></button>

The issue on chrome is, the browser doesn't wait for javascript remote event to come back. It refreshes the page (partially) straight away after making the ajax call.

Then I learnt that <button> tag in different browsers have different default types. So the fix to the above issue is simple. I just need to specify the button type. As in this code:

<button type="button">Assign Jobs</button>

However, this makes me curious about how Visualforce page respond to form post event. Let's suppose we do have a <button type="submit"> on the page. If I click that button, how would Visualforce page respond to it?

It seems to me that Visualforce page is submitting the <apex:form> content back to the server, but not the whole ViewState. Since the resulting page doesn't seem to have the state of the controller, nor does the constructor of the controller seem to be called. Is there a way in the controller we can define how we respond to these post events?

  • Lance, where is the VF code? I see angular stuff but no <apex:commandButton> – Sebastian Kessel Dec 2 '15 at 1:23
  • @SebastianKessel , although it is a VF page, we majorly use plain html tags instead of <apex:xxx> tags unless it is absolutely necessary. And yes you are right, I used angular js here. However, since this question is more about the sever side how the controller respond, I guess it is more of a Visualforce question than an angular question. – Lance Shi Dec 2 '15 at 1:37
  • The problem is that, normally, you need the apex tags for visualforce to properly deal with view state. Try replacing your button with one and you can evaluate the difference in behavior. (Not saying it's a permanent solution but it might help debug – Sebastian Kessel Dec 2 '15 at 2:41
  • @SebastianKessel That's not true. For ajax calls you don't need view state at all. Even if you do need that, actionFunction is still pretty handy. – Lance Shi Dec 2 '15 at 2:54
  • True, but you're not making an Ajax call, are you? If you are, then my bad. It sounded like you wanted a submit. – Sebastian Kessel Dec 2 '15 at 2:55

I have also experienced similar behavior by setting different type values.

  1. Type as Submit

button type="submit" onclick="callMe();">Click Me

Button click event will submit the form-data and page is partially refreshed (From client state).

  1. Type as Button

button type="button" onclick="callMe();">Click Me

However, setting type as "button", this is treated only as a clickable button, no form submission.

More Details : http://www.w3schools.com/tags/att_button_type.asp

| improve this answer | |
  • Thank you for that. Actually before posting this question, I have already gone through this page. However, I am still having two questions: 1, what does partially refreshed mean? What does it actually do underneath the surface. 2, when you submit the form, you actually submitted a post request to the server, so how does Salesforce controller process that request – Lance Shi Dec 2 '15 at 2:58
  • 1
    1. Partially refreshed mean page is refreshed from the client view state, it doesn't resolve the updated variables from backend. 2. I am also not much sure about this, but in my opinion, before the JS Remoting gets the response back, original page has been refreshed and it will lose the context. – Salesforcesmarty Dec 2 '15 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.