javascript Safari 和 Chrome 上的 window.location.href

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

window.location.href on Safari and Chrome

javascriptjquery

提问by Sammy D

This does not work in Safari and Chrome:

这在 Safari 和 Chrome 中不起作用:

$(".myButton").click(function(){
    window.location.href('www.blahblahblah.com');
});

what's the solution?

解决办法是什么?

回答by ntninja

window.location.href is not a function and therefor you cannot call it, you can however assign a value to it: (Also you should be using http:// when redirecting to another domain)

window.location.href 不是一个函数,因此你不能调用它,但是你可以给它赋值:(你也应该在重定向到另一个域时使用 http://)

$(".myButton").click(function(){
    window.location.href = 'http://www.blahblahblah.com';
});

EDIT: Corrected my first statement and added the part about http://

编辑:更正了我的第一条语句并添加了关于 http:// 的部分

回答by Josh

Had a similar problem. In chrome, return false; solved the problem.

有类似的问题。在chrome中,返回false;解决了这个问题。

if(confirm('Are you sure you wish to delete this order?'))
{
location.href='delete_order.asp?id=20'; **return false;**} 
else 
{return false;}

The above code works in Chrome and IE9

上面的代码适用于 Chrome 和 IE9

回答by Miles

window.location.assign('http://www.google.com')

is the function you're looking for.

是您正在寻找的功能。

It's the equivalent of

这相当于

window.location = 'http://www.google.com';