You need to sign in to do that
Don't have an account?
Passing JS variables using a Static Resource
I need to render my page as a PDF, so I need to move my inline JS to a static resouce. However, my JS references variables defined on the page. So, how do I reference dynamic content in a static resource?
This page works:
<apex:page standardcontroller="Account" tabStyle="Account">
<script type="text/javascript">
var var1 = '{!Account.name}';
function testFunction() {document.write(var1);}
testFunction();
</script>
</apex:page>
Same page rendered as PDF does not work, hence the need for a Static Resource:
<apex:page standardcontroller="Account" tabStyle="Account" renderAs="pdf">
<script type="text/javascript">
var var1 = '{!Account.name}';
function testFunction() {document.write(var1);}
testFunction();
</script>
</apex:page>
Test Static Resource code:
<script type="text/javascript">
var var1 = '{!Account.name}';
function testFunction()
{
document.write(var1);
}
testFunction();
</script>
New page code (does not work):
<apex:page standardcontroller="Account" tabStyle="Account">
<script type="text/javascript" src="{!$Resource.test}" />
</apex:page>
How do I define the Static Resource script var1 in my VF page?
Sorry for the incomplete answer. According to the Visualforce Dev Guide, you can't depend on JavaScript when rendering as a PDF.
All Answers
Drop the script tags in your static resource. In other words, the static resource should be this:
var var1 = '{!Account.name}';
function testFunction()
{
document.write(var1);
}
testFunction();
It'd also be better to reference the static resource using the apex:includeScript component in your page.
<apex:page standardcontroller="Account" tabStyle="Account">
<apex:includeScript value="{!$Resource.test}"/>
</apex:page>
Thank you--that helped, but now it's returning "{!Account.name}" instead of the actual account name (without renering as PDF). Also, when I add render as PDF, it still returns nothing. Any suggestions?
Sorry for the incomplete answer. According to the Visualforce Dev Guide, you can't depend on JavaScript when rendering as a PDF.