You need to sign in to do that
Don't have an account?
Ruchie
How to call the body- onload() javascript function in the VisualForce page.
Hi All,
Can anybody tell, how to call, the javascript onload() function in the <body> in a javascript. I wanted to call an alert statement, through a javascript on the onload function. Please help. Below is the code for reference:
<apex:page>
<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/11.1/connection.js"></script>
<apex:pageBlock>
<apex:form>
<body onload="init();">
<table width="100%">
<tr>
<td>
<input type="text" id="txtName" />
<input type="button" value="Save" />
</td>
</tr>
</table>
</body>
<script language="javascript">
sforce.connection.sessionId = '{!$Api.Session_ID}';
</apex:form>
<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/11.1/connection.js"></script>
<apex:pageBlock>
<apex:form>
<body onload="init();">
<table width="100%">
<tr>
<td>
<input type="text" id="txtName" />
<input type="button" value="Save" />
</td>
</tr>
</table>
</body>
<script language="javascript">
sforce.connection.sessionId = '{!$Api.Session_ID}';
function init()
{
alert('hi');
}
</script>
{
alert('hi');
}
</script>
</apex:form>
</apex:pageBlock>
</apex:page>
Try adding this script after your <apex:page> tag:
<script> var previousOnload = window.onload; window.onload = function() { if (previousOnload) { previousOnload(); } alert('loading...'); } </script>
This should give you a popup on page load to confirm that the script is working. Then swap out the alert() with the actual function you want to call.
Also, make sure the function you want to call has been defined or included prior to this script.
All Answers
Message Edited by JimRae on 12-28-2008 04:16 PM
I would recommend leveraging more of the VF component model in general - typically better ways to do things using components than raw HTML. For example, if you switch from external script includes to <apex:includeScript value="/soap/ajax/11.1/connection.js"/> VF will automatically take care of implementing once and only once inclusion in the <head> area.
I noticed that your init() declaration is not syntactically correct - you have the opening curly brace in the wrong location.
In your specific case a better approach to hooking into body onload would be to do something along these lines:
Also, it is highly unusual to need to use the ajax toolkit anymore in the context of visualforce - VF already provides rich bidirectional value binding, ajax/partial page update support, etc which remove the majority of use cases for the ajax toolkit in a modern salesforce application. Can you provide some detail on what you are trying to accomplish where you believe you will need to use the ajax toolkit?
Finally, be careful about what parts of salesforce you chose to reverse engineer - functions.js is an internal detail we do not support customers/partners using in their pages. Anything you might see in the generated source that is not specifically designated/documented as a public API is very much subject to change - e.g. functions.js is undergoing some radical work in Spring '09 that is almost guaranteed to result in your unsupported uses no longer functioning after the update.
Message Edited by dchasman on 12-27-2008 09:26 AM
I copied your final code and it does not work....
Message Edited by Olbrest on 01-20-2009 01:07 PM
Try adding this script after your <apex:page> tag:
<script> var previousOnload = window.onload; window.onload = function() { if (previousOnload) { previousOnload(); } alert('loading...'); } </script>
This should give you a popup on page load to confirm that the script is working. Then swap out the alert() with the actual function you want to call.
Also, make sure the function you want to call has been defined or included prior to this script.
An example of where I still need to use ajax is when i place a custom component inside an apex:repeat, and the component has, for example, a button that calls an actionfunction (that's also inside the component) with its rerender target being another element (that's also inside the component). That rerender target is NOT given a unique id; all occurences of the rerender target are given the same id for each time the component is redrawn in the apex:repeat), so the rerender fails. So, for things that need "repeating" with "rerendering", which in my case is quite a lot, I have to abandon the use of most visual force tags and go with plain old ajax and html.
Frankly, given what I described above, and the fact that you can't use apex:repeat to do thinks like populate rows in an apex:panelgrid, I think apex:repeat isn't ready for production release. I really wasted a lot of time with it, thinking it could do what it should do, and it just doesn't.
Instead of window.onload you could use add event listener (and attach event for IE9 and earlier) like so:
<apex:form>
<script>
window.onload = function () {
alert('hi');
}
</script>
This is your VF page.
</apex:form>
</apex:page>