2

I have created a vf page and added some javascript into that vf page which is using controller variable :

<apex:page controller="webstore">
<script>
    var username = '{!username}'
</script>
</apex:page>

It was working fine till the script was on vf page. Now, I have removed script from the vf page and created a static resource and have placed script into that. I have added that static resource into my page but now that script is not working. The script has been unable to identify that controller variable. It seems that controller variable doesn't work in this way. Is this a problem or I am missing something ?

5

Only Visualforce page (and component) files go through the binding to the controller process; static resource files do not.

If the amount of JavaScript is small then leaving it in the page is the simplest approach. If you have more JavaScript, you can create a JavaScript object in the page that has the controller values assigned, and reference that in the JavaScript that is in the static resource.

  • I have created those required variables into my vf page and have included script after that and it's resolved the issue. Just wanted to confirm if this is possible or not. Thanks!!! – S.Sharma Jun 5 '14 at 9:00
  • @S.Sharma then you should mark this answer as "Accepted" – New Alexandria Aug 25 '15 at 19:51

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.