Is there any way we can add Execute Javascript custom page button to Visualforce page and reuse it. I am just checking if any way we can save writing JS code is button again in VF page.

MY Visualforce page in Question is StandardController Page.

enter image description here

  • Do you want to have a VF page redirect from the Some JS code Goes here section?please clarify
    – Rao
    Jun 27, 2013 at 23:54
  • Nope its not JS redirection. it executes JS code Jun 28, 2013 at 0:02
  • Just a thought you question is probably better titled, "How to share JavaScript between Custom Buttons and Visualforce pages?" Jun 28, 2013 at 0:32
  • updated question Jun 28, 2013 at 2:15

2 Answers 2


You should be able to store your JavaScript as JS file in a Static Resource. Then use REQUIRESCRIPT (see trick and caviets here) in the Custom Button script and INCLUDESCRIPT in the Visualforce page. This should allow you to share your JavaScript.

Alternatively, depending on your skills and use case, you could also consider writing some shared Apex code either as a shared function between two controllers or a single controller used by both the Custom Button VF page and the other VF page/s in your solution.


There's an idea: Allow references to Static Resources on Custom Buttons with OnClick JavaScript. In it there's a link to this blog post Using static resources with Salesforce custom Javascript buttons that outlines a hack that would allow you to have a single source of the JavaScript reside in a static resource and refer to it from the custom button using the REQUIRESCRIPT function.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .