Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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)

share|improve this answer

You should use: window.location.href instead of window.location.

  • window.location is an object you shouldn't assign a string to object.
  • window.location.href is a property that tells you the current URL location of the browser.
 window.location.href = 'http://www.google.com'; //Will redirect you to Google immediately.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.