[removed].href doesn't redirect

后端 未结 10 1991
Happy的楠姐
Happy的楠姐 2020-12-18 17:40

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(\         


        
10条回答
  •  感动是毒
    2020-12-18 18:23

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

提交回复
热议问题