javascript window.location.href 不重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15759020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:00:44  来源:igfitidea点击:

window.location.href doesn't redirect

javascriptdom-eventswindow.location

提问by user2235124

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.

如果我没有注释警告或者如果我打开了 Mozilla 的 bugzilla 它工作正常,否则它不会重定向到另一个页面。

Any ideas why?

任何想法为什么?

回答by Jose

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.

如果您通过提交按钮调用此函数。这可能是浏览器不重定向的原因。它将运行函数中的代码,然后提交页面而不是重定向。在这种情况下,请更改按钮的类型标签。

回答by Sruit A.Suk

From this answer,

从这个答案,

window.location.href not working

window.location.href 不起作用

you just need to add

你只需要添加

return false;

at the bottom of your function

在你的函数底部

回答by komal thakkar

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-workingSolution:

http://www.codeproject.com/Questions/727493/JavaScript-document-location-href-not-working解决方案:

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

This worked for me.

这对我有用。

回答by Denys Séguret

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.locationassignation : this code isn't supposed to be executed as the page changes.

window.location分配后也删除所有内容:不应在页面更改时执行此代码。

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

注意:您不需要设置location.href. 只需设置location.

回答by Eldar

window.location.replaceis the best way to emulate a redirect:

window.location.replace是模拟重定向的最佳方式:

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

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

更多关于为什么window.location.replace是最好的 javascript 重定向的信息可以在这里找到。

回答by Luis Gouveia

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

确保您没有在 URL 末尾发送“#”。就我而言,这会阻止 window.location.href 工作。

回答by Ivijan Stefan Stipi?

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.locationproblem. Some browsers go into problem with window.location.hrefand also sometimes can happen that window.locationfail. That's why we also use window.location.replace()for any case and timeout for the "last try".

这是该window.location问题的通用工作解决方案。一些浏览器会出现问题,window.location.href有时也会发生window.location故障。这就是为什么我们也window.location.replace()为“最后一次尝试”使用任何情况和超时。

回答by Paulus

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

万一有人像我前几天晚上一样又累又傻,于是我遇到了许多支持不同方法来获得 javascript 重定向的线程,所有这些都失败了......

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

您不能使用window.location.replacedocument.location.href或任何您最喜欢的 vanilla javascript 方法将页面重定向到它自己

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;
}

回答by user12678669

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.

window.location.href 在 Android 中不起作用。我清除了 Android Chrome 中的缓存,它工作正常。建议在参与各种编码之前先尝试这个。

回答by Aijaj Hussain Ansari

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

在我的情况下,它在设置时间间隔后对所有浏览器都按预期工作。

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