0

I have an issue with Javascript prompt display. I'm trying to check if user is Guest from the variable {!type} which i get from my Apex controller on my OnClick event. However, the Javascript message box will appear even when the {!type} is not equal guest. Why is that so? Are there any issues with my JS function?

<script> 
    function confirmReg() {

                          if('{!type}' == 'GUEST'){ 
                                  var isCancel = confirm("Confirm add on?");    
                                  if (isCancel) window.location.href = "/goon";

                                 return false;

                              }
                          }  

                      </script>
                    <apex:commandButton action="{!add}" value="ADD"  styleClass="btn btn-lg btn-pri"  rendered="{!(CheckQty)}" onClick="return confirmReg();"/>
1

You should use quotes for the apex variable if it is a string. And don't forget == in a IF:

if('{!type}' == 'GUEST'){ 
  • Sorry @mast0r my mistake. Yes I've already edit that. However, the JS prompt won't display which is weird. – newbieVF Mar 19 '15 at 14:08
  • @newbieVF Have you checked the value of the type variable with for example console.log() ? – Sergej Utko Mar 19 '15 at 14:11
  • @newbieVF Another thing you should consider is that apex variables that were injected to the plain javascript are only initialize ONCE on the page load. Even if an apex variable has changed it value in the controller, it will still have old value on the page. You should then somehow reRender the javascript block to get a new value of the variable. – Sergej Utko Mar 19 '15 at 14:14
  • I think I know the issue. the problem lies in the URL. Cuz currently my URL has a parameter like this -> test2?pid=48. Therefore on page load, the value of variable {!type} does not get called. How do I assign or check if my var {!type} has a value? – newbieVF Mar 19 '15 at 14:16
  • @newbieVF Do console.log('type: {!type}') – Sergej Utko Mar 19 '15 at 14:17

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.