Find your content:

Search form

You are here

window.location not working

 
Share

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?


Attribution to: SFDC Geek

Possible Suggestion/Solution #1

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.

Attribution to: Ashwani

Possible Suggestion/Solution #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)


Attribution to: bigassforce
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33440

My Block Status

My Block Content