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
Hardik ChavdaHardik Chavda 

How can I call javascript function when the apex page load ?

Hi,
I have to call javascript function at the time of page loading. Following is my code and explaination of what I want to achieve.
<apex:page sidebar="false" controller="someController" >

<script>
function callThisFunctionAtThePageLoad(/*parameter will be passed from controller*/){
//alert('show this alert');
}
</script>

</apex:page>
So here, I want to pass one parameter in function for condition checking, which will come from the controller.
Best Answer chosen by Hardik Chavda
Hardik ChavdaHardik Chavda
Thanks Srikant for the reply but I actually got solution. and here it is. You were right first that I have to use jquery for that and that I did.
$( window ).ready(function() {
     pageload();
});
                                                                       

function pageload(){
      alert('test');
 }

 

All Answers

Shrikant BagalShrikant Bagal
Hello Hardik,

As Javascript is excuted as it order on Page i.e If you put you javascript code at end of all your code it will automatically get call after page Load.

If you are using Jquery try:

------------------------------------------------------------
$( document ).ready(function() {
console.log( "ready!" );
});
--------------------------------------------------------------------------
Please refer following doc for Jquery:
https://learn.jquery.com/using-jquery-core/document-ready/

If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks! 
Hardik ChavdaHardik Chavda
Hi Srikant,
Thanks for the quick reply but I don't get you when you said that I have to put javascript at the end of the page. And I only want to use javasript so If you could give me a code example then it would be great.
Shrikant BagalShrikant Bagal
Hi Hardik,


Suppose you want to access one of Input on Page load then you have to place you javascript after that input to access it into your javascript.

 
<apex:page sidebar="false" controller="someController" >
<input id="txtName" class="btn primary-btn" />


<script>
function callThisFunctionAtThePageLoad(/*parameter will be passed from controller*/){  
var elem = document.getElementById("mytext"); 
elem.value = "My default value";
}

callThisFunctionAtThePageLoad();
</script>

</apex:page>

As in above code you are calling "callThisFunctionAtThePageLoad" function after the <input> tag so you can access it in your javascript.

if you are trying to access it before the <input> tag you will get an error.


If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks! 

 
Hardik ChavdaHardik Chavda
Thanks Srikant for the reply but I actually got solution. and here it is. You were right first that I have to use jquery for that and that I did.
$( window ).ready(function() {
     pageload();
});
                                                                       

function pageload(){
      alert('test');
 }

 
This was selected as the best answer
Shrikant BagalShrikant Bagal
cool.
Please mark as best answer so it will help to other who will serve same problem.
T​hanks!