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
Charles McDowellCharles McDowell 

jquery navigation

Using a standard controller I am trying to use jQuery to redirect my page.  It does not work.  I am new to jQuery, can someone identify the problem


<apex:page standardController="Request_Chemical__c" sidebar="false" >
    <apex:includeScript value="$Resources.jQuery" />
    
    <script >
    var returnURL='/apex/Recommendation_Request_Dataview';
        function redirectPage(){
              $(location).attr('href' returnURL);
        }
    </script>
    
    <apex:form >
        <apex:pageBlock title="Chemical Exposure" >
           <apex:pageBlockSection >
            <apex:inputField value="{!Request_Chemical__c.Recommendation_Request__c}" /><br/>
            <apex:inputField value="{!Request_Chemical__c.Chemical__c}" /><br/>
            <apex:inputField value="{!Request_Chemical__c.Percentage__c}" />
        </apex:pageBlockSection>
        
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" oncomplete="redirectPage" value="Save" />
               <apex:commandButton action="{!Cancel}" value="Back" /> 
         </apex:pageBlockButtons>

        </apex:pageBlock>
 
    </apex:form>
  
</apex:page>
Timothy Gentet O'BrienTimothy Gentet O'Brien
You have a couple of issues...
  1. <apex:includeScript value="$Resources.jQuery" /> needs to have the Merge Field Syntax or it will not load the jQuery script properly, it needs to be: <apex:includeScript value="{! $Resources.jQuery }" />
  2. $(location).attr('href' returnURL); is missing the comma, it needs to be: $(location).attr('href', returnURL);
    1. You can also should also try and use the $Page merge field syntax, it makes your redirect more secure and will automatically recognise if you are in Classic, Lightning, or even SF1 Mobile and should always redirect correctly, to do that update this to: $(location).attr('href', '{! $Page.Recommendation_Request_Dataview }');
You should be good to go then! :)
Charles McDowellCharles McDowell
I made the changes but it still does not work.  I set the following test page and this does not redirect the page. Any thoughts.

<apex:page >
     <apex:includeScript value="{! $Resource.jQuery }"/>
    
    <script >
    var returnURL='/apex/Recommendation_Request_Dataview';
        function redirectPage(){
              $(location).attr('href', '{$Page.Recommendation_Request_Dataview}');
        }
    </script>
    
    
    <apex:form>
    
        <apex:commandButton value="Move" onclick="redirectPage" />
    
    
    
    </apex:form>
</apex:page>