+ Start a Discussion

Visualforce page to run javascript onload?

is it possible to have javascript run upon opening a visual page? I know I can get the apex controller constructor to run, but I want javascript to access the browser url.


The following only works in development mode:

<script type="text/javascript">
    var detailURL= parent.document.referrer;
    detailURL = detailURL.substring(detailURL.lastIndexOf('/')+1);

    function init() {
        if ((detailURL > "" ) && (detailURL.length == 15)) {
            alert('pushing '+detailURL);
    parent.onload = init; //window.onload;


Best Answer chosen by Admin (Salesforce Developers) 

Use this instead:



<BODY onLoad="init()"/>



All Answers


Use this instead:



<BODY onLoad="init()"/>



This was selected as the best answer

Try out sample code given below :


            window.onload = new function() { checkCondition(); };

            function checkCondition()


              // your logic here



Hope this helps.