I have an HTML5 app setup; when the browser is closed, the session ends. When I re-open the app, the app detects the session is invalid, and prompts the user to login again. This is all fine.
However, when the user is redirected back to the page, their old session information is still cached:
sforce.connection.sessionId = '{!$Api.Session_ID}';
Thus, the application still sees the old session and thinks the app is offline, even though the user has a new session and is online.
Is there a way to reset the sessionId dynamically on the cached page?
Attribution to: user3014439
Possible Suggestion/Solution #1
You could have some Javascript run when the page loads, that rerenders the bit of script containing {!$Api.Session_ID}
, like this
<apex:page>
<script type="text/javascript">
// Need to call actionFunction indirectly, since it isn't defined
// early enough to add it to window below
function doRerenderPanel() {
console.log('Rerendering panel');
rerenderPanel();
}
// Or $(document).ready() if you're using jQuery
if (window.addEventListener) {
window.addEventListener('load', doRerenderPanel, false);
} else if(window.attachEvent) {
window.attachEvent('onload', doRerenderPanel);
}
</script>
<apex:form>
<apex:actionFunction name="rerenderPanel" rerender="scriptPanel" />
<apex:outputPanel id="scriptPanel">
<script>
// sforce.connection.sessionId = '{!$Api.Session_ID}';
alert('Your session id: {!$Api.Session_ID}');
</script>
</apex:outputPanel>
</apex:form>
</apex:page>
In the example, the alert fires twice - once when the page is first rendered, and again when the outputPanel is rerendered. You will need some logic to ignore the initial value of {!$Api.Session_ID}
and only use the rerendered version.
Attribution to: metadaddy
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31562