[removed].href doesn't redirect

后端 未结 10 1970
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:22

    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.

    0 讨论(0)
  • 2020-12-18 18:23

    From this answer,

    window.location.href not working

    you just need to add

    return false;
    

    at the bottom of your function

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2020-12-18 18:28

    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.

    0 讨论(0)
  • 2020-12-18 18:29

    In case anyone was a tired and silly as I was the other night whereupon I came across many threads espousing the different methods to get a javascript redirect, all of which were failing...

    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;
    }
    
    0 讨论(0)
  • 2020-12-18 18:31

    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.

    0 讨论(0)
提交回复
热议问题