+ Start a Discussion
Nisar AhmedNisar Ahmed 

Clicking a button on a VF page on Load.

Hi All,


I have two VF pages 'A' and 'B'.

Page 'A' contains a textbox and 'Search' button, on clicking 'Search' button it directs us to Page 'B' with the search string as a parameter.

Page 'B' contains a 'Go Back' button, on clicking 'Go Back' button it redirects us back to Page 'A' with the same search string as a parameter.



When I click on 'Go Back' button on Page 'B', I need to pass that search string in the textbox and click on 'Search' button automatically(or onload).


Please let me know if you any ideas to solve this problem or bug.


I'd suggest that you would do better to define a page action attribute on page a, and have that check the URL to see if the search string has been passed as an url parameter.  If it has, it can take the appropriate action to run the search and when the page is rendered the results will appear automatically.

Nisar AhmedNisar Ahmed

Hi Bob,


Thanks for replying to my posts.

Now, I am facing a new problem.



After passing the string to the textbox, I am unable to click the 'Search' button or (submit the form) so that i can get back the search results when we click on the 'Go Back' button on page 'B'.

Here is the script that I have written on Page 'A' :


/* Script code */
function gup( name )
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
            return "";
            return results[1];

function Submitfunction()
        var btnSubmitTags = document.getElementById('j_id0:form1:searchsubmit');
        var store_str = gup( 'StoreSearch' );
        oFormObject = document.forms['j_id0:form1'];
        oFormElement = oFormObject.elements['addressInput'];
        if(store_str.length > 0){
            oFormElement.value = store_str;

/* Script code */

 Please let me know if you have any idea about this problem or where I have went wrong. I have also posted a new post about the if you don't remember the whole scenario. Thanks again.


I have replied to your other post, but I'll reply to this one also.

Have you tied your submit button to an onload handler or similar?  E.g. when Page A is rendered and the search parameter is present in the URL, do you then invoke the SubmitFunction javascript?

Nisar AhmedNisar Ahmed

Yes Bob, I have invoked the 'Submitfunction' onload in the <body> tag as shown :



<apex:form id="form1">
<body onload ="Submitfunction();document.getElementById('addressInput').focus();initialize();">

 Still I am unable to get the search results.



A couple of things:


(1) Have you added some alerting into your SubmitFunction to check that it is progressing as you expect.

(2) Have you considered using an action attribute on the page declaration? This would allow the search to be run server side before the page was rendered.  I've found that to be a lot easier to set up than javascript submission at load time.