45

I know this is a question much discussed but I can't figure out why it does not work for me.

This is my function:

function ShowComments(){

 alert("fired");
 var movieShareId = document.getElementById('movieId');
 //alert("found div" + movieShareId.textContent || movieShareId.innerText);
 //alert("redirect location: /comments.aspx?id=" + movieShareId.textContent || movieShareId.innerText + "/");
 window.location.href = "/comments.aspx?id=" + movieShareId.textContent || movieShareId.innerText + "/";
 var newLocation = window.location;
 //alert("full location: " + window.location);

}

If I have the alerts uncommented or if I have Mozilla's bugzilla open it works fine, otherwise it does not redirect to the other page.

Any ideas why?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2235124
  • 471
  • 1
  • 4
  • 4
  • 2
    What's the point of ` var newLocation = window.location;` ? No line should be executed after the window.location change. – Denys Séguret Apr 02 '13 at 08:06
  • How are you calling the function? Are you doing something else at that point that may interfer with the change of location? – Guffa Apr 02 '13 at 08:08
  • Does this answer your question? [How to prevent mailto event from opening a new tab in browser](https://stackoverflow.com/questions/13457684/how-to-prevent-mailto-event-from-opening-a-new-tab-in-browser) – Vega Apr 02 '23 at 17:33

11 Answers11

93

If you are calling this function through a submit button. This may be the reason why the browser does not redirect. It will run the code in the function and then submit the page instead of redirect. In this case change the type tag of your button.

RononDex
  • 4,143
  • 22
  • 39
Jose
  • 931
  • 6
  • 3
  • 8
    Thanks. Helped a lot. I had my button 'type' as 'submit'. Changed it to 'reset' and it worked. – richie Mar 07 '14 at 09:49
  • Exctly, the problem of this sorts, is often due to the click or 'enter'-event bubbeling up, and submitting the form. – Alex Jan 07 '15 at 19:12
  • 3
    You absolutely made my day. 12h of google and stackoverflow searching for that. Top. – MDaldoss Sep 17 '16 at 07:36
  • 2
    Interestingly enough, even if my button was not `submit`, it didn't work cause it was inside a form. I had to move it outside then it worked. – NecipAllef Oct 26 '16 at 06:02
  • 1
    This was my issue for a button that had NO type call out, putting type='button' fixed the issue. Thanks! – Ryan Gibbs Sep 06 '17 at 23:16
  • I was using `onclick` in a link but wasn't working. Changing link to `button` worked! – curtisp Feb 13 '20 at 20:42
  • Dude thank you so much, I've been trying to find this for hours! – Kevin Sullivan Sep 12 '20 at 15:08
  • The default behavior of a `` if inside a `
    ` is always the `submit` behavior. In order to do front-end manipulations in ReactJs land, using the `` is the solution! Thanks ! [See here](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type)
    – etiennejcharles May 03 '21 at 21:59
  • 1
    To add here, if you still want to have `button[type=submit]` and handle the redirection in the `form`'s `submit` event, you can also call `event.preventDefault()` in the event handler--setting `window.location.href` should then properly redirect. – sleighty Jul 27 '22 at 22:45
32

From this answer,

window.location.href not working

you just need to add

return false;

at the bottom of your function

Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71
9

Some parenthesis are missing.

Change

 window.location.href = "/comments.aspx?id=" + movieShareId.textContent || movieShareId.innerText + "/";

to

 window.location = "/comments.aspx?id=" + (movieShareId.textContent || movieShareId.innerText) + "/";

No priority is given to the || compared to the +.

Remove also everything after the window.location assignation : this code isn't supposed to be executed as the page changes.

Note: you don't need to set location.href. It's enough to just set location.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • @user2235124 There are other problems in your page, I think. And the fact you have some code after the location assignment makes me suspect bigger logical problems. Can you build a [fiddle](http://jsbin.com) showing your issue ? – Denys Séguret Apr 02 '13 at 08:22
8

Solution from here: http://www.codeproject.com/Questions/727493/JavaScript-document-location-href-not-working

document.location.href = 'Your url',true;
Dmitry Shvedov
  • 3,169
  • 4
  • 39
  • 51
komal thakkar
  • 199
  • 2
  • 7
5

Make sure you're not sending a '#' at the end of your URL. In my case, that was preventing window.location.href from working.

Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68
5

You can't use window.location.replace or document.location.href or any of your favourite vanilla javascript methods to redirect a page to itself.

So if you're dynamically adding in the redirect path from the back end, or pulling it from a data tag, make sure you do check at some stage for redirects to the current page. It could be as simple as:

if(window.location.href == linkout)
{
    location.reload();
}
else
{
    window.location.href = linkout;
}
Dmitry Shvedov
  • 3,169
  • 4
  • 39
  • 51
Paulus
  • 89
  • 1
  • 9
4

I'll give you one nice function for this problem:

function url_redirect(url){
    var X = setTimeout(function(){
        window.location.replace(url);
        return true;
    },300);

    if( window.location = url ){
        clearTimeout(X);
        return true;
    } else {
        if( window.location.href = url ){
            clearTimeout(X);
            return true;
        }else{
            clearTimeout(X);
            window.location.replace(url);
            return true;
        }
    }
    return false;
};

This is universal working solution for the window.location problem. Some browsers go into problem with window.location.href and also sometimes can happen that window.location fail. That's why we also use window.location.replace() for any case and timeout for the "last try".

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
3

window.location.replace is the best way to emulate a redirect:

function ShowComments(){
    var movieShareId = document.getElementById('movieId');
    window.location.replace("/comments.aspx?id=" + (movieShareId.textContent || movieShareId.innerText) + "/");
}

More information about why window.location.replace is the best javascript redirect can be found right here.

Community
  • 1
  • 1
Eldar
  • 591
  • 3
  • 16
2

In my case it is working as expected for all browsers after setting time interval.

setTimeout(function(){document.location.href = "myNextPage.html;"},100);
0

window.location.href wasn't working in Android. I cleared cache in Android Chrome and it works fine. Suggest trying this first before getting involved in various coding.

0

Go to your api route, make sure you are not missing a response such as

res.status(200).json(data); 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 22 '23 at 17:48