0

I've never used window.location in my VF pages.

I was reading about lightning where it was mentioned that window.location won't work if vf page is used with lightning.

I first decided to check how window.location works in Salesforce classic but issue is , I'm not able to make it work even there.

In my simple test vf page having just input elements and having standard Controller(Account), if I introduce following script tag in head tag at top:

<script>
window.location.href = "http://yahoo.com";
</script>

Then on saving and loading again, it shows just blank page, with no change in Browser URL

I started thinking problem could be i m trying accesing outside URL .So also tried changing to salesforce URL of another VF page :

window.location.href =
'https://Myorg-dev-ed--c.ap1.visual.force.com/apex/Casedetail?id=' +
 caseid

This also doesn't work

Then I tried calling this URL from Command button something like this :

function redirectBack(){
var caseid ='5009000000singc';
window.location.href = 'https://Myorg-dev-ed--c.ap1.visual.force.com/apex/Casedetail?id=' + caseid ;
alert(window.location.href); // Surprise, surprise, this alert also shows existing page URL
}

<Apex:pageblockButtons >
       <apex:commandbutton value="save" onclick="redirectBack();" />
</Apex:pageblockButtons>

To my surprise, this also redirects back to same page (My current vf page, vftest) and javascript alert after window.location.href also shows the URL Of same page, even though I'm properly assigning correct URL in just previous line .

this alert shows: https://My-dev-ed--c.ap1.visual.force.com/apex/vftest?core.apexpages.devmode.url=1

Please note 'Vftest' is the name of my current page on which I'm doing this testing

What I'm doing wrong ?

  • what do you mean window.location.href not redirecting? – Ratan Paul Dec 26 '15 at 13:08
1

WHen you use

function redirectBack(){
var caseid ='5009000000singc';
window.location.href = 'https://Myorg-dev-ed--c.ap1.visual.force.com/apex/Casedetail?id=' + caseid ;
alert(window.location.href); // Surprise, surprise, this alert also shows existing page URL
}

it will assign the window.location.href but first it will complete the current page javascript execution then after it will redirect

  • Hi Ratan... the question is, why redirect is not happening ? I've given a new location(URL), then it should open that URL ..rt? this is what.. not happening. – sfdc_Qrius Dec 26 '15 at 16:50
  • @sfdc_Qrius window.location.href should work. if not then 2 reason can be--1:- There is another scripting error that preventing your window.location.href to execute 2:- May be you are running this code in inline vf page. else it should work. I have tested above code it is working for me. – Ratan Paul Dec 27 '15 at 6:19
-2

Instead of using window.location, try window.open('/'+caseid,'_top');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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