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
Prateek Aneja 2Prateek Aneja 2 

Using Javascript in Static Resource

This is the code I have written in Visualforce to access a Javacsript file:-

<apex:page sidebar="false">
    <apex:includeScript value="{!$Resource.Stat1}"/>
</apex:page>

But it is not working.

Javascript Code:

<html>
    <body>    
    <script>
        document.getElementById("demo").innerHTML = "Hello Javascript";
    </script>

    <p id = "demo"></p>
    </body>    
</html>

 
Naval Sharma4Naval Sharma4
Hi Pratik,

Instead of putting HTML elements in your static resource, you are supposed to keep JS code only.
So I would suggest you to stick with following approach. Salesforce automatically adds HTML and Body tags.


<apex:page sidebar="false">
    <p id ="demo"></p>
    <apex:includeScript value="{!$Resource.Stat1}"/>
</apex:page>
Dario BakDario Bak
Try this:

<script> var previousOnload = window.onload; window.onload = function() { if (previousOnload) { previousOnload(); } document.getElementById("demo").innerHTML = "Hello Javascript"; } </script>