如何在Visualforce标记中导入javascript文件?

时间:2014-08-01 11:13:49

标签: javascript salesforce visualforce

如何在Visualforce标记中导入文件?我尝试使用URLFOR函数在Salesforce中引用静态资源。

以下脚本有效

<apex:page>
<html>
<body>

<h1>My First JavaScript</h1>

<p>Click Date to display current day, date, and time.</p>

<button type="button" onclick="myFunction()">Date</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = Date();
}
</script>

</body>
</html> 
</apex:page>      

但是,我想通过引用VisualForce中的函数来实现以下功能

  <apex:page>
  <html>
  <body>
<h1>My First JavaScript</h1>

<p>Click Date to display current day, date, and time.</p>

<button type="button" onclick="myFunction()">Date</button>

<p id="demo"></p>


<apex:includeScript value="{!URLFOR($Resource.SalesDashboard, '/js/datefunction.js')}"/>

</body>
</html> 
</apex:page>  

我也尝试过:

      <apex:page>
      <html>
      <body>
    <h1>My First JavaScript</h1>

    <p>Click Date to display current day, date, and time.</p>

    <button type="button" onclick="myFunction()">Date</button>

    <p id="demo"></p>


   <apex:includeScript value="{!$Resource.SalesDashboard}"/>

    </body>
    </html> 
    </apex:page>  

1 个答案:

答案 0 :(得分:2)

根据this salesforce link

在Visualforce页面中包含JavaScript的最佳方法是将JavaScript放在静态资源中,然后从那里调用它。例如,

<apex:includeScript value="{!$Resource.SalesDashboard}"/>