3

Is it possible to include script and meta tags before the the standard salesforce tags?

The standard head is like the following:

<head>
<script src="/jslibrary/1351189248000/sfdc/JiffyStubs.js"></script>
<style type="text/css"></style>
<script src="/jslibrary/1351189248000/sfdc/SfdcCore.js" type="text/javascript">
</script><script src="/static/111811/js/picklist.js" type="text/javascript"></script>
<script src="/jslibrary/1351189248000/sfdc/VFState.js" type="text/javascript"></script>

Now first my modifications follow:

<link class="user" href="/resource/16456546546/Foo/css/main.css" rel="stylesheet" type="text/css">

<meta http-equiv="PRAGMA" content="NO-CACHE">
<meta http-equiv="Expires" content="Mon, 01 Jan 1990 12:00:00 GMT">
<meta content="IE=EmulateIE8" http-equiv="X-UA-Compatible">
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<script src="/resource/1354808845000/SCRM/script/jquery.min.js" type="text/javascript"></script> <script src="/resource/1354546456/Foo/script/jquery-ui.min.js" type="text/javascript"></script>

 <title>Site Login</title> </head>

I need to be able to insert something before the first Salesforce standard script tag. E.g. according to http://boards.developerforce.com/t5/Visualforce-Development/Struggling-with-IE/td-p/506213.

2
  • Try wrapping your code in a {} code comment, since it's not visible here.
    – Adam
    Commented Dec 13, 2012 at 17:16
  • have you tried adding your meta tag from a build script included in the page that will inject the tags you need by creating the elements dynamically?
    – Ronnie
    Commented Jun 29, 2018 at 1:37

3 Answers 3

5

Using the <apex:includeScript /> and <apex:stylesheet /> tags instruct the VF parser to place the contents into the rendered document <head> block. It does not matter where these tags are physically placed in the VF (or component) markup, the result will be a script or stylesheet reference within the <head> block at a known location.

Scripts are normally injected into the markup just after SfdcCore.js and the stylesheet is injected just after the SalesForce 'theme' stylesheet definition.

Unfortunately, this same feature of conveniently rendering items into the head doesn't exist for <meta> tags within the document using an apex tag. If you define the meta tags within the body, you're creating invalid markup per the spec, though most browsers tolerate this without complaint.

Scripts which have been included using the <apex:includeScript /> tag more than one time in the VF markup will not include multiple references to the script in the rendered markup. I presume this uniqueness aspect exists in the <apex:stylesheet /> tag as well.

As a side-note, be aware that adding <html> and <head> and <title> tags directly in your VF markup may result in stray tags in the rendered document (multiple html or head tags, etc). This would also be considered invalid markup.

Depending on what your requirement is to 'insert something before the first Salesforce standard script tag', you can use JavaScript to insert elements before it in the DOM like so:

var js = document.createElement('script');
js.src = 'myscript.js';
var first = document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(js, first);
2

To prevent invalid markup through having multiple head tags, and to be able to inject content into the head tags of a visualforce page I suggest you look at the apex:page attribute of applyhtmlTag. If you set this to false, you can manually define the head tags yourself and add whatever you need between them.

This also allows you to add meta tags at build time rather than after the page is loaded.

0

if you are referring to VisualForce pages, I have had to do this quite a few times. I usually keep all such tags just after the <Apex:page> tag.

Something like:

<apex:page sidebar="false" showheader="false" standardstylesheets="false">
<apex:includeScript value="{!URLFOR(................)}" />
<apex:stylesheet value="{!URLFOR(...................')}" />

<html>

    <head>
         <link>...</link>
         <title>Customer Portal Home</title>

         <style>
         </style>
        </head>

       <body>
       </body>
</html
2
  • hey Richard, is there any way to include <apex:includeScript> in the VF page and at the end, this script will be loaded at the end of the body tag instead of inside head tag? Commented Nov 4, 2016 at 13:20
  • 1
    Hi @danigonlinea: Use the loadOnReady="true" attribute with the includeScript tag to defer loading on the scripts. This way the scripts will be moved towards the bottom of the page, before the </body> tag. Example: <apex:includeScript loadOnReady="true" value="{!URLFOR($Resource.jQuery3,'/jquery-3.5.1.slim.min.js')}"/> Commented Sep 23, 2020 at 7:33

You must log in to answer this question.

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