+ Start a Discussion
pgriffepgriffe 

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?

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveAnderson41SteveAnderson41

Sorry for the incomplete answer.  According to the Visualforce Dev Guide, you can't depend on JavaScript when rendering as a PDF.

 

 

All Answers

SteveAnderson41SteveAnderson41

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>

 

 

 

Message Edited by SteveAnderson41 on 03-12-2009 10:05 AM
Message Edited by SteveAnderson41 on 03-12-2009 10:06 AM
pgriffepgriffe

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?

Message Edited by pgriffe on 03-12-2009 10:34 AM
Message Edited by pgriffe on 03-12-2009 10:37 AM
SteveAnderson41SteveAnderson41

Sorry for the incomplete answer.  According to the Visualforce Dev Guide, you can't depend on JavaScript when rendering as a PDF.

 

 

This was selected as the best answer
pgriffepgriffe
Ahh.  That is very sad news, but at least I know to quit trying.  Thanks so much for your help!