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
T_BET_BE 

javascript onload in body tag is not working in visualforce page, running in different containers?

Hello,

 

I want to call a function in the body tag (onload = ""). So I wrote the following visualforce page. But the alert is not opening.

 

<apex:page showHeader="true">

<head>

</head>
<body onload="alert('asd');">
</body>

</apex:page>

 

If i change the showHeader = "false" then the code is working.

What are the differences between this two codes? They are running in different containers???

 

<apex:page showHeader="false">

<head>

</head>
<body onload="alert('asd');">
</body>

</apex:page>

 

asish1989asish1989

Hi
I think when you write showHeader = "false" then some inbuilt java script library is loaded, that's why alert is showing .
If you want to show alert by writing showHeader = "true" then you need to write in this way...

<apex:page showHeader="true">
    <head>

   </head>
   <script>
    
       var previousOnload = window.onload;        
       window.onload = function() { 
           if (previousOnload) { 
               previousOnload();
           }
           alert('asd...');

       }
    </script>

</apex:page>

 

Did this post answers your question, please accept as solutions and also don't forget to hit kudos button If this post really helps you.

Thanks