Using JavaScript in Visualforce Pages

Using JavaScript in Visualforce pages gives you access to a wide range of existing JavaScript functionality, such as JavaScript libraries, and other ways to customize the functionality of your pages. Action tags, such as <apex:actionFunction> and <apex:actionSupport>, support Ajax requests.

By including JavaScript in a page, you are introducing the possibility of cross-browser and maintenance issues that you do not have when using Visualforce. Before writing any JavaScript, you should be sure that there is not an existing Visualforce component that can solve your problem.

Warning

The best method for including JavaScript in a Visualforce page is placing the JavaScript in a static resource, then calling it from there. For example,
<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>
You can then use the functions defined within that JavaScript file within your page using <script> tags.

When using JavaScript within an expression, you need to escape quotes using a backslash (\). For example,

onclick="{!IF(false, 'javascript_call(\"js_string_parameter\")', 'else case')}"

Tip