[SalesForce] Stop object detail page from refreshing at click of a button on embedded visualforce page

At the window.onload() event of a standard object detail page I am storing the time value in one of its fields. I have embedded a vf page along with this detail page. At the click of a button in the embedded vf page shown,the object detail page refreshes and the window.onload event resets the value to the new time stored in the object field. Is there a way to stop the detail page from refreshing?

Just adding snippets of the code here :

<script>
    window.onload = function() 
    {
        sforce.connection.sessionId = "{!$Api.Session_ID}";
        var aResult = sforce.connection.query("SELECT Time__c FROM card__c where Id = {!card__c.Id}");
        /*code to update the time filed on the object*/
</script>
<script>
    function abc()  
    {
        /*Some DML Operations here*/
    }
</script>
    <apex:form>
        <apex:pageBlock>
           <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Do Something" onclick="abc" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>  
</apex:page>

Best Answer

You need to add return false; to your onclick function. Without that the page will reload after clicking.

Another solution is to add an rerender="none" tparameter to the command button.

So it will look like this:

<apex:commandButton value="Do Something" onclick="abc(); return false;" />

or like this:

<apex:commandButton value="Do Something" onclick="abc(); reRender="none" />
Related Topic