[removed].href doesn't redirect

2019-02-16 07:44发布

I know this is a question much discussed but I cant 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?

7条回答
冷血范
2楼-- · 2019-02-16 08:19

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.

查看更多
贼婆χ
3楼-- · 2019-02-16 08:19

From this answer,

window.location.href not working

you just need to add

return false;

at the bottom of your function

查看更多
Lonely孤独者°
4楼-- · 2019-02-16 08:25

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.

查看更多
SAY GOODBYE
5楼-- · 2019-02-16 08:28

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".

查看更多
Explosion°爆炸
6楼-- · 2019-02-16 08:34

Though it is very old question, i would like to answer as i faced same issue recently and got solution from here -

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

document.location.href = 'Your url',true;

This worked for me.

查看更多
趁早两清
7楼-- · 2019-02-16 08:35

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

查看更多
登录 后发表回答