function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Add javascript to page layout without creating a visual force page?

I have a custom object with a number of record types, for each of which I have created a specific page layout. What I want to do is add a little javascript (Jquery actually) to hide/show certain fields depending on the value of other fields. Is it possible to do that without converting all the pages to visual force pages?


I am very happy with the pages that SF generates automatically and I don't really want to start creating my own in VF, as this will entail quite a lot of work in creating them and maintaining them.


I did have the idea that I could embed a visual force page in the page layouts, and all that VF page contained was some javascript to add the functionality I wanted. But this did not seem to work. Maybe I just didn't do it right.


Thanks in advance




The problem is that VF runs on a different domain name than the UI, so you can't normally just call scripts at will from the VF page to the parent window. Also, there's no HTML5 hooks that you can call from VF to manipulate the UI. However, there is a trick you can use. Take the following steps:


1) Create a custom home page component, using "HTML Area (narrow)" as the type (Setup > Customize > Home > Home Page Components).

2) Create a home page layout that uses this component (Setup > Customize > Home > Home Page Layouts).

3) Assign the layout to all users.

4) Enable "show custom sidebar components on all pages" (Setup > Customize > User Interface).

5) Done!


The trick is actually in step 1. You see, inside an HTML Area, you can place a script tag. Inside this script tag, you could check the URL of the page you're on, and if it matches "\/001.*\/e" (a regular expression), then you can perform whatever jQuery setup you'd like to do. The sidebar HTML area can only hold a limited amount of code, but you could use that to load a static resource if necessary.


I gave this a quick go at the time of my original post, and it seemed to work, but now I've come back to it in earnest, it is not working. Any script tags and code that I paste into the HTML area gets stripped out when I hit save. Is there a way round that?


Bump....running into the same issue with script tags. Been searching for a couple days, and found many people questioning this, but no answer.


Yes, it's frustrating isn't it? Eventually I realised I really did have to create a VF page, and I found it wasn't as time consuming as I expected once I got stuck in. Not trivial, but not nearly as much work as the equivalent in would be for example. The apex:inputfield controls and others make it quite easy to reproduce the look and feel of the native GUI. then you can use apex:actionsupport to add ajax behaviour, and you can also quite easily add JQuery or other javascript functionality.



Well the issue I have is that I'm working with two separate Visualforce pages on the Account page layout. I want an action on one of the pages to refresh the other page. Since they're in iframes, it causes security issues, so I was thinking of handling it with a Home Page Component. This doesn't seem to be the answer, however.


Is HTML5 appropriate? One could use session storage to proprogate events across iframes without the mess of a middleman...