[SalesForce] window.location not working

I am stuck in a situation, hope so I could get some help here.

I'm having a condition where I want to redirect a page to one URL. It is related to service cloud console.

In controller I defined a merge variable :

public string finalURL {get; set;}

VF page :

<apex:commandButton action="{!save}" value="Save" onComplete="refreshTab({!isError});"/>

and I am using this memebr variable in Javascript.

Javascript code :

<script type="text/javascript">

           function refreshTab(isError) {
               if (!isError) {
                   sforce.console.getEnclosingPrimaryTabId(refreshTabById);
               }
           }
           function refreshTabById(tabId) {
               if(sforce && sforce.console && sforce.console.isInConsole()) {
                   sforce.console.refreshPrimaryTabById(tabId.id, true);
               } else {
                   var urlString = {!finalURL}; //'https://cs14.salesforce.com/1234'
                   window.location = urlString;
               }
           }      

       </script>

But, the issue is, when code is coming to the else condition, the page is not redirecting to the new URL defined. Any idea?

Best Answer

Your custom button code is throwing a JavaScript exception.

This is because {!finalURL} will dump an unescaped URL out into the middle of your source code - you will want to put quotes '' around him like so, and escape for good measure also:

var urlString = '{!JSENCODE(finalURL)}'; //https://cs14.salesforce.com/1234
window.location.href = urlString;

(Regal has a good point too: window.location will still work, but it's better practise to use href)

Related Topic