Javascript 重定向 window.location.href - IE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4578691/
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-25 14:11:16  来源:igfitidea点击:

Javascript Redirect window.location.href - IE

javascript

提问by ABailiss

Trying to do a redirect with javascript using the following code (works fine in ALL browsers except IE8 and IE7):

尝试使用以下代码使用 javascript 进行重定向(在除 IE8 和 IE7 之外的所有浏览器中都可以正常工作):

if (confirm('Are you sure you wish to clear your basket?')) {
    window.location.href = '/basket/clear';

    if (navigator.userAgent.search('MSIE') > -1) alert('Your basket has been cleared.');
}

I have added in the second alert stating that the basket has been cleared so that the code works in IE. If I remove the alert then it doesn't work.

我在第二个警告中添加了说明篮子已被清除以便代码在 IE 中工作的警告。如果我删除警报,则它不起作用。

Any ideas or solutions would be greatly appreciated.

任何想法或解决方案将不胜感激。

回答by hunter

try using just window.location = '/basket/clear';

尝试使用 window.location = '/basket/clear';

also, when you change the window location you cannot expect code beyond that point to execute, such as an alert

此外,当您更改窗口位置时,您不能期望超出该点的代码执行,例如 alert