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
Tim__mTim__m 

Delivering JavaScript with Visualforce and Sites

So I have been asked to rebuild and move everything on our IIS box to Salesforce Sites. Yeah, I know, crazy right. I'm not the decision maker, I'm just the schlep that needs to deliver on the decision makers promises.

 

Anyway, one problem that I don't know how to solve is delivering my JavaScript widgets with Visualforce. Can it be done? Can I do something like this...

 

<apex:page contentType="application/javascript">

Basically I need to prove that I can or can't host my js files on Salesforce in a way that other sites can access them, you know, something like the Google CDN or Yahoo CDN.

 

Any input will be reward with my gratitude.

 

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

There are a two ways that I can see to do this:

 

1. Put all your JS into a VF page:

 

<apex:page contentType="text/javascript" cache="false" expires="0">
function MyScript() {
     alert('Yahoo! No more static resource uploads!);
}

</apex:page>

 

And expose that page via sites. 

 

2.  If you click on the download link for a static resource (and you select the checkbox that makes the static resource publicly accessible) you'll get the url that it's served from. This URL won't change for a specific resource.

 

Not ideal I know but it'll work.

 

Wes 

All Answers

BulentBulent

yes, you can upload them as static resources and reference them in your public site page

Take a look at this article, it's gives some examples.

Tim__mTim__m

I don't think I did a good job describing the use case. I have a library of JavaScript widgets. The widgets are not for my pages but for the customer's pages. The customer's pages are on their own domains with their own servers or hosting providers. On the customer's page they do something like this...

 

...
<body>
<div>Stuff here</div>
<script src=http://mydomain.com/widgets/1.0/myscript.js></script>
</body>
...

 

The "myscript" file adds content where the script tag is placed when the page is called by the client. The "myscript.js" file is a JavaScript file on my domain served from my IIS box. This is known as XSS or cross site scripting (let's avoid the debate about XSS and security for now). Google Adsense works exactly the same way.

 

So the question is, can I distribute my JavaScript file from Sites to other domains? A static resource doesn't solve the problem, unless I'm missing your point. The Delivering Static Resources with Visualforce wiki is good stuff but it only address delivering static resources for consumption on the Visualforce page that references it.

 

But still, much gratitude to you Bulent!

 

 

 

Message Edited by Tim__m on 02-25-2010 11:04 AM
WesNolte__cWesNolte__c

Hey

 

There are a two ways that I can see to do this:

 

1. Put all your JS into a VF page:

 

<apex:page contentType="text/javascript" cache="false" expires="0">
function MyScript() {
     alert('Yahoo! No more static resource uploads!);
}

</apex:page>

 

And expose that page via sites. 

 

2.  If you click on the download link for a static resource (and you select the checkbox that makes the static resource publicly accessible) you'll get the url that it's served from. This URL won't change for a specific resource.

 

Not ideal I know but it'll work.

 

Wes 

This was selected as the best answer
Tim__mTim__m

Thanks Wes, that's what I'm looking for.

Aravind MSAravind MS

It is a very good idea, but when we use for loop or conditional statement we'll have '<' or '>'. During that time, it throws the following error,

 

 Element type "object" must be followed by either attribute specifications, ">" or "/>"  Error: Element type "object" must be followed by either attribute specifications, ">" or "/>".

 

Any suggestion to fix it?