2

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?

2 Answers 2

4

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)

3

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.
3
  • This doesn't correctly answer the question. Replacing window.location with window.location.href does not fix the problem.
    – robinCTS
    Commented Aug 1, 2017 at 14:04
  • The other issue is that the first bullet point is wrong. Technically, window.location is not an object, but a property with getter and setter methods. The getter returns the Location object associated with the window's document, whilst the setter is effectively an alias to the window.location.href property…
    – robinCTS
    Commented Aug 1, 2017 at 14:06
  • … You are not actually assigning a string to an object but setting a property to a string. A subsequent fetch of the location property will still return a Location object, albeit now with a different href property. It works this way for historical reasons. Maybe you could say that you shouldn't use this form as it looks like you are assigning a string to an object property.
    – robinCTS
    Commented Aug 1, 2017 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .