function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DodiDodi 

Visual Force global variable to be used in multiple java script blocks

Is it possible to create a java script variable in visual force that can be used in multiple areas.

 

For instance if I have multiple page block and java script sections in a VF page, I want to declare a variable once in Java Script and use it in multiple page blocks. Currently I have to declare them multiple times within the individual page block section, I would like to declare it once and use it multiple java script blocks in my page.

 

How do I declare this?

 

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kum09kum09

Hi,

 

I faced the same problem when I was using javascript variable in VF Page. I solved the issue by declaring the variable outside all page blocks.

<script>
        var dtRequestedDate;

</script>

 

Now in any page Block write a function

            function getValue()
            {
                dtRequestedDate = document.getElementById("{!$Component.dtpicker}").value;
            }

Now in whichever PageBlock you want to use this variable.... Call the getValue() function in the script and then use the variable.. (dtRequestedDate).

 

Then you will be able to get the values of the javascript variable in all the pageblock.

 

Regards,

ckumar

All Answers

kum09kum09

Hi,

 

I faced the same problem when I was using javascript variable in VF Page. I solved the issue by declaring the variable outside all page blocks.

<script>
        var dtRequestedDate;

</script>

 

Now in any page Block write a function

            function getValue()
            {
                dtRequestedDate = document.getElementById("{!$Component.dtpicker}").value;
            }

Now in whichever PageBlock you want to use this variable.... Call the getValue() function in the script and then use the variable.. (dtRequestedDate).

 

Then you will be able to get the values of the javascript variable in all the pageblock.

 

Regards,

ckumar

This was selected as the best answer
priyanshipriyanshi

is it possible to declare a javascript variable and then use it across multiple visualforce pages?