+ Start a Discussion
TehNrdTehNrd 

Force.com Sites, iframes, and navigation

First a little background. Our company already has a website that is managed the old fashion way by our IT department. It is massive, complicated, and there is no way on earth we (I am in a separate group from IT) could convince them to convert to Force.com sites. The website has the two typical navigation bars, one along the top and one on the left. So a much more realistic possibility is using an iframe to embed a force.com site/web app within our site.

Here lies the problem. For an iframe that does not need to navigate anywhere we are good, but based on the use case and logic we may want to redirect the users to different pages. I was hoping that in the the page controller, based on the logic, I could set a string which is a URL and then on the completion of this action method call a javascript method. When doing this it appears that the oncomplete attribute of the action method is actually called before all of the variables are set in the controller.

Here is an example:

Code:
Page:

<apex:page controller="iframe">
    <apex:form id="form">
        <apex:inputText value="{!one}"/> + <apex:inputText value="{!two}"/> = 8 <br/>
        <apex:commandButton value="Validate" action="{!submit}" rerender="form" oncomplete="done();"/>
        
        <script type="text/javascript">
            function done(){
                var result = '{!result}';
                alert(result);
                <!-- here I could use window.top.location.href = result to redirect the entire page -->
} </script> </apex:form> </apex:page> Controller: public class iframe { public Integer one {get; set;} public Integer two {get; set;} public String result {get; set;} /*In a real world example based on the logic I may want to pass the navigation URL*/ public PageReference submit() { if(one + two == 8){ result = 'You know math!'; // }else{ result = 'Keep trying!'; } system.debug(result ); return null; } }

As you can see if returns the value of the previous action.

1) Enter 4 + 4, it will return null
2) Enter 4 + 2, it will return 'You know math!'

Thanks,
Jason



Message Edited by TehNrd on 12-03-2008 04:36 PM
Best Answer chosen by Admin (Salesforce Developers) 
RaizersRaizers
Update... I located and solved the problem. 
The ampersand "&" above the script tag created the problem.  Once the "&" was removed, the redirect worked in FireFox. 
 
Code:
<apex:page standardController="Contact" extensions="MyExtension">

// TESTING - Verify Redirection Link
Dynamic href URL is:  {!gohere}&serverUrl={!$Api.Partner_Server_URL_120}

<script>
window.location.href='{!gohere}&serverUrl={!$Api.Partner_Server_URL_120}';
</script>
</apex:page>

 

All Answers

Ron HessRon Hess
I think you can get your page to redirect to the desired location using a combination of rerender , rendered and window.location.href

so, in your command button, don't use oncomplete="", rather use rerender

the section that you rerender will have another output panel that is controlled by a rendered="{!doredirect}" expression

in your controller, you have doredirect off, until you have all the math done correctly, then set doredirect to true

in the (now rendered) output panel, you have a snippet of javascript to redirect

pseduo code:

Code:
<apex:commandButton value="Validate" action="{!submit}" rerender="form2" />


<apex:outputPanel id="form2"  rendered=" {!doredirect} " >
<script>window.location.href = 'where you want to go';</script>
</apex:outputPanel>

 
you can then use top to move the correct window as needed.

hope this helps.
TehNrdTehNrd
Very slick, it works!

Only thing I don't like is that now I have logic in my VF page that ideally I would like to have in my controller.
RaizersRaizers
I'm using <script>window.location.href = 'where you want to go';</script> on a VF page to redirect to an external site.  If I place the entire URL (i.e., window.location.href='https://somewebsite.com/apage?name=john )  the redirect works in both IE and Firefox.
However, if I combined this with a variable using a controller (i.e., window.location.href='{!gohere}'  )  then it only redirects when using IE, but fails with FireFox.  In the FireFox Error Console there is an error msg:  "Permission denied to get property Location.pathname"  and indicates the VF page I was using.
 
Can anyone verify this on their machine?  
Any suggestions on a fix or work around ?


Message Edited by Raizers on 12-12-2008 03:10 AM
RaizersRaizers
Update... I located and solved the problem. 
The ampersand "&" above the script tag created the problem.  Once the "&" was removed, the redirect worked in FireFox. 
 
Code:
<apex:page standardController="Contact" extensions="MyExtension">

// TESTING - Verify Redirection Link
Dynamic href URL is:  {!gohere}&serverUrl={!$Api.Partner_Server_URL_120}

<script>
window.location.href='{!gohere}&serverUrl={!$Api.Partner_Server_URL_120}';
</script>
</apex:page>

 
This was selected as the best answer