[SalesForce] Javascript alert on page load based on the value of controller variable

I am getting the value of booleanvariable in controller as below. I want to display an alert on visual force page when boolean variable is true.I see the value of Booleanvariable is true in the url,but alert is not triggering.

public with sharing class TestBoolean {

boolean pbv;

public TestBoolean(){    

     try{     pbv = boolean.valueOf(system.CurrentPageReference().GetParameters().get('BooleanVariableName'));
  }catch(Exception e){ 
  pbv = false;
 }

}

If I just run alert without If condition and no parameter, its displaying alert.
Any help is appreciated.

Java script for alert on visual force page

window.onload = showAlert(!pbv);
function showAlert(!pbv) 
{
  if(!pbv) {
    alert('Hi I am the alert you have been waiting for');
  }
}

Best Answer

All you need to do is this for the JS (Global merge variables)

window.onload = showAlert();
function showAlert() 
{
  if('{!$CurrentPage.parameters.BooleanVariableName}' == 'true') {
    alert('Hi I am the alert you have been waiting for');
  }
}

Additionally (Not needed for above) if you want to use the PBV on the page you need to assign getters and setters

public Boolean PBV {get; set;}

then use merge fields on the page:

{!PBV}

Also, you should use the first example in my answer to your previous question. The try catch in not applicable in your case

Related Topic