3

Is there any way to make a force.com site write JavasScript only?

I have the following which ALMOST gets me what I want, but there's I still get the <head> tag as part of the document. Is there a way to remove that <HEAD> tag?

<apex:page showHeader="false" 
    sidebar="false"
    controller="custom_controller"
    standardStylesheets="false"
    applyBodyTag="false"
    applyHtmlTag="false"
    docType="html-5.0"
>
<script type="text/javascript">

    <apex:repeat value="{!events}" var="event">
        document.write('<li><apex:outputText value="{!event.name}"></apex:outputText></li>');
    </apex:repeat>    
</script>
</apex:page>
  • I'm confused. You have a <script> tag but the repeater is emitting HTML. – Mike Chale Dec 9 '13 at 17:52
  • 1
    @MikeChale Actually, they're trying to write a JS file that has a bunch of document.write calls in it. – sfdcfox Dec 9 '13 at 18:50
  • Yes, that's correct. I was trying to include data from SF in our website. At first I had an iframe, but I couldn't control the height of the iframe. Now the VF page is actually writing the content and the height won't be an issue. – penaservices Dec 9 '13 at 22:49
7

You specified HTML5, so you're emitting HTML5. Instead, use this set of markup:

<apex:page showHeader="false" sidebar="false" controller="custom_controller"
    standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false"
    showHeader="false" contentType="text/javascript">
    <apex:repeat value="{!events}" var="event">
        document.write('<li><apex:outputText value="{!event.name}"></apex:outputText></li>');
    </apex:repeat>
</apex:page>

Specifying DOCTYPE makes the page an HTML page, not JavaScript. If your intent is to use it as a JavaScript file (e.g. via includeScript), you must not specify DOCTYPE, and you must specifically inhibit any HTML rendering with the appropriate tags, and you must also set the content type to JavaScript so it will interpret correctly. Also, JavaScript files never use a script tag, because they are assumed to be JavaScript already-- script is an HTML construct that tells the HTML renderer that the contents are JavaScript within an HTML page.

  • Thanks! The only issue in your code is showSidebar="false" should only read sidebar="false" – penaservices Dec 9 '13 at 22:53
  • @penaservices I always mistype that one. Fixed it for the future. – sfdcfox Dec 9 '13 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.