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

How to add to the <HEAD> dynamically

We ran into a problem of needing to add a <META> tag to the <HEAD> of the brower in Salesforce.  You can add Javascrpt with the <apex:includeScript>, but there is current no documented way of adding other tags such as <META>


Here is a trick that I developed and appears to be working nicely:


I added a JavaScript file to the <HEAD> using <apex:includeScript value="{!$Resource.stevefix}"/>


The file: stevefix contains:



 function addToHEAD() 


  newScript = document.createElement('meta'),

  newScript .setAttribute('name', 'flag #1');

  newScript .setAttribute('content', 'true');


  var meta = document.getElementsByTagName('SCRIPT')[0];


  meta.parentNode.appendChild(newScript );







This works by:


#1 The JS file is added in the head.

#2 Inside, it defines a function:  addToHEAD()

#3 It calls the function addToHEAD.


Inside the function, I use the DOM to find the first instance of <SCRIPT>.  This could actually be the JS itself, or another one from SF.


It creates a new tree node, populates it with the proper values.


Then it adds it to the parent of the found <SCRIPT>. (adds as a sibling of the found script).


Using this method, we may be able to add whatever we need to the <HEAD>.


Good luck and I hope that it helps.


Steve Simpson

Ethos - 

I read this post and was about to try this because I have exactly the same issue.  I need to be able to get meta tags within the head tags.  After poking around the discussion boards a little more (because javascript is really not my strong suit) I saw a sample page where the author had included head and body tags within the Apex page tags.  I tried it and it works beautifully.  It seems that if you do not include the head and body tags, the apex page support inserts them for you, but if they are already there, it just works with what you have.